Skip to content

Commit

Permalink
Enable kaleido_configuration Changes Triggering Updates for kaleido_n…
Browse files Browse the repository at this point in the history
…ode and kaleido_service (#35)

* Updating Configurations w/ details_json

Signed-off-by: hfuss <[email protected]>

* make target for m1 mac

Signed-off-by: hfuss <[email protected]>

* fix for dev docs

Signed-off-by: hfuss <[email protected]>

* updating dev docs to prevent future mistakes

Signed-off-by: hfuss <[email protected]>

* Adding update_trigger field for kaleido_node; fixing unit tests

Signed-off-by: hfuss <[email protected]>

* fix update_trigger

Signed-off-by: hfuss <[email protected]>

* adding last_updated to configuration; update_trigger for services

Signed-off-by: hfuss <[email protected]>

* ensuring last_updated reports as computed after apply to provide consistent plans

Signed-off-by: hfuss <[email protected]>
  • Loading branch information
onelapahead authored May 4, 2022
1 parent 6e176f6 commit f31401c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 4 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ vendor
.LSOverride

terraform-provider-kaleido*
coverage.txt
coverage.txt

# Folder for defining Terraform scripts to manually test the provider
test/
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ LDFLAGS="-X main.buildDate=`date -u +\"%Y-%m-%dT%H:%M:%SZ\"` -X main.buildVersio
DEPS=https://gmplib.org/download/gmp/gmp-6.0.0a.tar.bz2
TARGETS="windows-10.0/*,darwin-10.10/*"

.PHONY: test

all: deps build test
build:
$(GOBUILD) -o ${BINARY_NAME}
Expand All @@ -28,6 +30,8 @@ deps:
build-linux:
GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(BINARY_LIN) -v
build-mac:
GOOS=darwin GOARCH=arm64 $(GOBUILD) -o $(BINARY_MAC) -v
build-mac-legacy:
GOOS=darwin GOARCH=amd64 $(GOBUILD) -o $(BINARY_MAC) -v
build-win:
GOOS=windows GOARCH=amd64 $(GOBUILD) -o $(BINARY_WIN) -v
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@ make

To install the provider from a local build with Terraform 0.14, configure your `~/.terraformrc` with:

```
```hcl
provider_installation {
dev_overrides {
"registry.terraform.io/kaleido/kaleido" = "/path/to/terraform-provider-kaleido"
"registry.terraform.io/kaleido-io/kaleido" = "/path/to/terraform-provider-kaleido"
}
direct {}
}
```

then be sure to build the binary you are testing using:

```shell
make build
```

> NOTE: binaries built via `make build-${OS}` will not be detected by Terraform's `dev_overrides`.
## Examples

End to end example in [examples/multi_region_with_b2b](examples/multi_region_with_b2b)
Expand Down
38 changes: 37 additions & 1 deletion kaleido/resource_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package kaleido
import (
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/customdiff"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -61,7 +62,18 @@ func resourceConfiguration() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"last_updated": {
Type: schema.TypeInt,
Computed: true,
},
},
CustomizeDiff: customdiff.All(
customdiff.ComputedIf("last_updated", func(d *schema.ResourceDiff, meta interface{}) bool {
return d.HasChange("name") || d.HasChange("details") || d.HasChange("details_json") ||
d.HasChange("type") || d.HasChange("membership_id") ||
d.HasChange("environment_id") || d.HasChange("consortium_id")
}),
),
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Expand Down Expand Up @@ -111,6 +123,7 @@ func resourceConfigurationCreate(d *schema.ResourceData, meta interface{}) error
}

d.SetId(configuration.ID)
d.Set("last_updated", time.Now().UnixNano())

return nil
}
Expand All @@ -119,7 +132,15 @@ func resourceConfigurationUpdate(d *schema.ResourceData, meta interface{}) error
client := meta.(kaleido.KaleidoClient)
consortiumID := d.Get("consortium_id").(string)
environmentID := d.Get("environment_id").(string)
details := duplicateDetails(d.Get("details").(map[string]interface{}))
detailsMap := d.Get("details").(map[string]interface{})
detailsJSON := d.Get("details_json").(string)
if detailsJSON != "" {
if err := json.Unmarshal([]byte(detailsJSON), &detailsMap); err != nil {
msg := "Could not parse details_json of %s %s in consortium %s in environment %s: %s"
return fmt.Errorf(msg, d.Get("type"), d.Get("name"), consortiumID, environmentID, err)
}
}
details := duplicateDetails(detailsMap)
configuration := kaleido.NewConfiguration(d.Get("name").(string), "", "", details)
configID := d.Id()

Expand All @@ -135,6 +156,8 @@ func resourceConfigurationUpdate(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf(msg, configID, consortiumID, environmentID, status, res.String())
}

d.Set("last_updated", time.Now().UnixNano())

return nil
}

Expand Down Expand Up @@ -164,6 +187,19 @@ func resourceConfigurationRead(d *schema.ResourceData, meta interface{}) error {

d.Set("name", configuration.Name)
d.Set("type", configuration.Type)
d.Set("details", configuration.Details)

// if details_json is set, we need it to reflect the state for diffing
detailsJSON := d.Get("details_json").(string)
if detailsJSON != "" {
detailsJSON, err := json.Marshal(configuration.Details)
if err != nil {
msg := "Could not parse configuration details to JSON for config %s in consortium %s in environment %s with status %d: %s"
return fmt.Errorf(msg, configurationID, consortiumID, environmentID, status, res.String())
}
d.Set("details_json", string(detailsJSON))
}

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions kaleido/resource_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ func resourceNode() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
},
"update_trigger": {
Type: schema.TypeString,
Optional: true,
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Expand Down
4 changes: 4 additions & 0 deletions kaleido/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ func resourceService() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
},
"update_trigger": {
Type: schema.TypeString,
Optional: true,
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Expand Down

0 comments on commit f31401c

Please sign in to comment.