Skip to content

Commit

Permalink
Upgraded to terraform plugin framework (big rewrite)
Browse files Browse the repository at this point in the history
* Framework wip

* optional fields

* clientclaim type works

* it works (except emtpy gives null)

* handle empth clientClaims

* Handle empty values as empty list instead of nil

* ClientSecret working

* Update main.go

* name changes

* handle update for clientsecret

* User client basics

* 1

* Refactor

* userclient

* Fix for always having changes to ClientNameLanguageKey

* convertStringArrayToSet

* Comments about fields not checking server drift

* resource_apiscope wip

* ApiScope works

* typos

* logging

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* "protocol_versions": ["6.0"]

* Update terraform-tester.tf
  • Loading branch information
joachimhalvorsen authored Sep 20, 2024
1 parent c985264 commit 21ffb8e
Show file tree
Hide file tree
Showing 30 changed files with 1,811 additions and 1,429 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ terraform-tester/custom-log.text
terraform-tester/.terraform.lock.hcl
.terraform.lock.hcl
/.idea
custom-log.text
/logs
2 changes: 2 additions & 0 deletions Folder.DotSettings.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=lvia/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
57 changes: 35 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ See [here](https://learn.hashicorp.com/collections/terraform/providers) for the

# Local Setup
## Install go
[Golang installation guide](https://golang.org/doc/install)
[Go installation guide](https://golang.org/doc/install)

## Install terraform
[Install terraform](https://learn.hashicorp.com/terraform/getting-started/install.html). For Windows, you can add terraform.exe to {user}/bin. Make sure %USERPROFILE%\go\bin is in path, and above the go-specific paths.
Expand All @@ -28,14 +28,12 @@ Checkout the code-repo to {GOPATH}\src\github.com\3lvia\terraform-provider-elvid

# Project structure
* repo-root
* internal
* provider: classes for elvid-provider and resources (machineclient, userclient, clientsecret and apiscope)
* elvidapiclient: classes for getting AccessToken from AD and calling ElvID-api
* terraform-tester.tf and versions.tf: Terraform files for manually testing the provider.
* elvidapiclient: go class-library for getting AccessToken from AD and calling ElvID-api
* main.go: Standard file, sets up serving of the provider by calling the Provider()-function.
* provider.go: Defines the provider schema (inputs to the provider), the mapping to resources, and the interface that is passed to resources
* resource_clientsecret.go: Defines the resource schema and methods for clientsecrets.
* resource_machineclient.go: Defines the resource schema and methods for machineclient.
* resource_userclient.go: Defines the resource schema and methods for userclient.
* resource_apiscope.go: Defines the resource schema and methods for apiscope.
* elvidapiclient: go class library for getting AccessToken from AD and calling ElvID-api
* main.go: Standard file, sets up serving of the provider.

# Setup terraform for running locally
## Setup dev overrides to target local build of the provider
Expand Down Expand Up @@ -89,6 +87,9 @@ Make sure you have set up Terraform for running locally (described above)
```console
# from repo-root
terraform apply;

# Similar with auto apply
terraform apply -auto-approve;
```

You should get a warning on plan / apply
Expand All @@ -101,27 +102,39 @@ You don't usually need to run terraform init because we are using dev_overrides.
If you are working with modules, you might have to do terraform init (it will tell you when running plan or apply).
Terraform init will download the published library from terraform registry, but the dev_overrides variant will still be used on plan/apply.

## Build and apply with one command
# Logging and diagnostics
Providers use Diagnostics to surface errors and warnings to Terraform.
For debugging or informational purposes use logging instead.

More info about [diagnostics](https://developer.hashicorp.com/terraform/tutorials/providers-plugin-framework/providers-plugin-framework-logging).

Regular logging can be done with tflog with methods for Debug, Info, Warn and Error.
More info about [logging](https://developer.hashicorp.com/terraform/tutorials/providers-plugin-framework/providers-plugin-framework-logging).

Example logging string
```console
# from repo-root
go build; terraform apply -auto-approve;
tflog.Warn(ctx, "Some message to be logged")
```

# Debugging
Debugging the go-code when running from Terraform is not added to this repo. See [this guide if debugging should be considered](https://developer.hashicorp.com/terraform/plugin/sdkv2/guides/v2-upgrade-guide#support-for-debuggable-provider-binaries).
Example logging object as json
```console
serialized, _ := json.Marshal(someObject)
tflog.Warn(ctx, string(serialized))
```

It is possible to print debug info as warnings in diag.Diagnostics. This is used for ApiScope and MachineClient. It requires v2 of the SDK, and some rewrite of the resource definition, as in resource_apiscope.go/apiscopeservice.go. See [the upgrade guide for v2 of the SDK](https://www.terraform.io/docs/extend/guides/v2-upgrade-guide.html). Terraform-privider-elvid already uses v2, but v2 also supports the v1 way.
If you don't se the logs locally, you probably need to change log level first.
```console
# Linux
TF_LOG=INFO
# Windows:
$Env:TF_LOG="INFO"
```

For resources/services that is not yet rewritten to v2 (but still use error and Create instead of CreateContext), debugging can be done by writing a file with debug messages:
# Debugging
Debugging is now supported (but not tested by us): https://developer.hashicorp.com/terraform/plugin/framework/debugging

```
# for a string
ioutil.WriteFile("custom-log.text", []byte(someString), 0644)
Up til now, we have only used logging to understand what is goin on during a run.

# for an object
serialized, _ := json.Marshal(someObject)
ioutil.WriteFile("custom-log.text", []byte(serialized), 0644)
```
# Publish a new release
## Publish to Terraform Registry
To publish to [registry.terraform.io/providers/3lvia/elvid](https://registry.terraform.io/providers/3lvia/elvid/latest) create a new github-release in this repo.
Expand Down
161 changes: 0 additions & 161 deletions elvidapiclient/apiscopeservice.go

This file was deleted.

58 changes: 0 additions & 58 deletions elvidapiclient/httputils.go

This file was deleted.

Loading

0 comments on commit 21ffb8e

Please sign in to comment.