diff --git a/.gitignore b/.gitignore index 6d473a7..d5298dd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.tf *.tfstate* +main.tf diff --git a/go.mod b/go.mod index 3d455fd..64d218d 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/cycloidio/terraform-provider-cycloid go 1.21.5 require ( - github.com/cycloidio/cycloid-cli v1.0.98-0.20240305164015-c12162039e5c + github.com/cycloidio/cycloid-cli v1.0.98-0.20240314113358-3129ff260090 github.com/gogo/protobuf v1.3.2 github.com/hashicorp/terraform-plugin-framework v1.4.2 github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 diff --git a/go.sum b/go.sum index 19a8d0e..415b5de 100644 --- a/go.sum +++ b/go.sum @@ -108,6 +108,10 @@ github.com/cycloidio/cycloid-cli v1.0.98-0.20240201134523-5b2eeaab1a05 h1:BPvpYm github.com/cycloidio/cycloid-cli v1.0.98-0.20240201134523-5b2eeaab1a05/go.mod h1:eI5O+rpBYCJQxy0NFu5UgU5AzFG+RkCTTi3kr/RHReg= github.com/cycloidio/cycloid-cli v1.0.98-0.20240305164015-c12162039e5c h1:5adsFyer3S8T1PiTXLe/ejD1TepIqQMRDVQamd6eiHI= github.com/cycloidio/cycloid-cli v1.0.98-0.20240305164015-c12162039e5c/go.mod h1:eI5O+rpBYCJQxy0NFu5UgU5AzFG+RkCTTi3kr/RHReg= +github.com/cycloidio/cycloid-cli v1.0.98-0.20240314102650-e003b99bb4d3 h1:xjU+kUCeqWvVQHgoOK0TLVq8bAmSoAe2BF2cEhNe51E= +github.com/cycloidio/cycloid-cli v1.0.98-0.20240314102650-e003b99bb4d3/go.mod h1:eI5O+rpBYCJQxy0NFu5UgU5AzFG+RkCTTi3kr/RHReg= +github.com/cycloidio/cycloid-cli v1.0.98-0.20240314113358-3129ff260090 h1:boSOhTkk2CsTOayHXWPqAAAbF/SPK5kt9FJOa6WmUDM= +github.com/cycloidio/cycloid-cli v1.0.98-0.20240314113358-3129ff260090/go.mod h1:eI5O+rpBYCJQxy0NFu5UgU5AzFG+RkCTTi3kr/RHReg= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/main.tf b/main.tf deleted file mode 100644 index 9773c0e..0000000 --- a/main.tf +++ /dev/null @@ -1,28 +0,0 @@ -terraform { - required_providers { - cycloid = { - source = "registry.terraform.io/cycloidio/cycloid" - } - } -} - -resource "cycloid_credential" "tf_credential" { - name = "tf test 2" - path = "path" - type = "aws" - body = { - access_key = "access" - secret_key = "secret" - } -} - -provider "cycloid" { - url = "https://api.staging.cycloid.io/" - jwt = "eyJhbGciOiJIUzI1NiIsImtpZCI6IjJmMjEyMmRlLTYzZjItNGVlYy05YzZmLWM2YWJiM2UxZjAwNyIsInR5cCI6IkpXVCJ9.eyJjeWNsb2lkIjp7InVzZXIiOnsiaWQiOjAsInVzZXJuYW1lIjoidGYtdGVzdCIsImdpdmVuX25hbWUiOiIiLCJmYW1pbHlfbmFtZSI6IiIsInBpY3R1cmVfdXJsIjoiIiwibG9jYWxlIjoiIn0sImFwaV9rZXkiOiJ0Zi10ZXN0Iiwib3JnYW5pemF0aW9uIjp7ImlkIjoxMiwiY2Fub25pY2FsIjoic2VyYWYiLCJuYW1lIjoiQ3ljbG9pZCBzdGFnaW5nIiwiYmxvY2tlZCI6W10sImhhc19jaGlsZHJlbiI6ZmFsc2UsInN1YnNjcmlwdGlvbiI6eyJleHBpcmVzX2F0IjotNjIxMzU1OTY4MDAsInBsYW4iOnsibmFtZSI6IkludmFsaWQiLCJjYW5vbmljYWwiOiJpbnZhbGlkIn19LCJhcHBlYXJhbmNlIjp7Im5hbWUiOiIiLCJjYW5vbmljYWwiOiIiLCJkaXNwbGF5X25hbWUiOiIiLCJsb2dvIjoiIiwiZmF2aWNvbiI6IiIsImZvb3RlciI6IiIsImlzX2FjdGl2ZSI6ZmFsc2UsImNvbG9yIjp7ImIiOjAsImciOjAsInIiOjB9fX0sImhhc2giOiI4NDEyMTBmMGM2YzI5Mzg1NjViMzQwYjYzYzRlNDA3NWM2ZjhjZGU1In0sInNjb3BlIjoiYXBpLWtleSIsImF1ZCI6ImN1c3RvbWVyIiwianRpIjoiNTkwOGNiNTgtNDM4Yy00MmYwLWEwZmEtM2JkNzJkMmRmNmQzIiwiaWF0IjoxNzA2NjA5Nzk5LCJpc3MiOiJodHRwczovL2N5Y2xvaWQuaW8iLCJuYmYiOjE3MDY2MDk3OTksInN1YiI6Imh0dHBzOi8vY3ljbG9pZC5pbyJ9.tCM8CLk5-eAnLR1e4-6VIajahYfspXo3aCiAd_kB9Vw" - organization_canonical = "seraf" -} - -#resource "cycloid_organization" "child_test" { -#name = "terraform organization test" -#} - diff --git a/provider/credential_resource.go b/provider/credential_resource.go index 50ae62a..4520ae2 100644 --- a/provider/credential_resource.go +++ b/provider/credential_resource.go @@ -9,6 +9,7 @@ import ( "github.com/cycloidio/cycloid-cli/cmd/cycloid/middleware" "github.com/cycloidio/terraform-provider-cycloid/provider_cycloid" "github.com/cycloidio/terraform-provider-cycloid/resource_credential" + "github.com/davecgh/go-spew/spew" "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/types" @@ -53,7 +54,6 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ // Read Terraform plan data into the model resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) - if resp.Diagnostics.HasError() { return } @@ -81,7 +81,9 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ can := data.Canonical.ValueString() des := data.Description.ValueString() - cred, err := mid.CreateCredential(r.provider.OrganizationCanonical.ValueString(), name, ct, rawCred, path, can, des) + orgCan := getOrganizationCanonical(r.provider, data.OrganizationCanonical) + + cred, err := mid.CreateCredential(orgCan, name, ct, rawCred, path, can, des) if err != nil { resp.Diagnostics.AddError( "Unable create credential", @@ -90,7 +92,7 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ return } - credentialCYModelToData(ctx, r.provider.OrganizationCanonical.ValueString(), cred, &data) + credentialCYModelToData(ctx, orgCan, cred, &data) // Save data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) @@ -112,7 +114,9 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest, can := data.Canonical.ValueString() - cred, err := mid.GetCredential(r.provider.OrganizationCanonical.ValueString(), can) + orgCan := getOrganizationCanonical(r.provider, data.OrganizationCanonical) + + cred, err := mid.GetCredential(orgCan, can) if err != nil { resp.Diagnostics.AddError( "Unable read credential", @@ -121,7 +125,7 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest, return } - credentialCYModelToData(ctx, r.provider.OrganizationCanonical.ValueString(), cred, &data) + credentialCYModelToData(ctx, orgCan, cred, &data) // Save updated data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) @@ -165,7 +169,10 @@ func (r *credentialResource) Update(ctx context.Context, req resource.UpdateRequ can = plandata.Canonical.ValueString() } - cred, err := mid.UpdateCredential(r.provider.OrganizationCanonical.ValueString(), name, ct, rawCred, path, can, des) + orgCan := getOrganizationCanonical(r.provider, data.OrganizationCanonical) + spew.Dump(data.OrganizationCanonical) + + cred, err := mid.UpdateCredential(orgCan, name, ct, rawCred, path, can, des) if err != nil { resp.Diagnostics.AddError( "Unable create credential", @@ -174,7 +181,7 @@ func (r *credentialResource) Update(ctx context.Context, req resource.UpdateRequ return } - credentialCYModelToData(ctx, r.provider.OrganizationCanonical.ValueString(), cred, &data) + credentialCYModelToData(ctx, orgCan, cred, &data) // Save updated data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) @@ -192,11 +199,13 @@ func (r *credentialResource) Delete(ctx context.Context, req resource.DeleteRequ can := data.Canonical.ValueString() + orgCan := getOrganizationCanonical(r.provider, data.OrganizationCanonical) + // Delete API call logic api := common.NewAPI(common.WithURL(r.provider.Url.ValueString()), common.WithToken(r.provider.Jwt.ValueString())) mid := middleware.NewMiddleware(api) - err := mid.DeleteCredential(r.provider.OrganizationCanonical.ValueString(), can) + err := mid.DeleteCredential(orgCan, can) if err != nil { resp.Diagnostics.AddError( "Unable delete credential", diff --git a/provider/organization_resource.go b/provider/organization_resource.go index 2d1ace9..4795941 100644 --- a/provider/organization_resource.go +++ b/provider/organization_resource.go @@ -3,6 +3,7 @@ package provider import ( "context" + "github.com/cycloidio/cycloid-cli/client/models" "github.com/cycloidio/cycloid-cli/cmd/cycloid/common" "github.com/cycloidio/cycloid-cli/cmd/cycloid/middleware" "github.com/cycloidio/terraform-provider-cycloid/provider_cycloid" @@ -56,7 +57,9 @@ func (r *organizationResource) Create(ctx context.Context, req resource.CreateRe api := common.NewAPI(common.WithURL(r.provider.Url.ValueString()), common.WithToken(r.provider.Jwt.ValueString())) mid := middleware.NewMiddleware(api) - co, err := mid.CreateOrganizationChild(data.Name.ValueString(), r.provider.OrganizationCanonical.ValueString()) + orgCan := getOrganizationCanonical(r.provider, data.OrganizationCanonical) + + co, err := mid.CreateOrganizationChild(data.Name.ValueString(), orgCan) if err != nil { resp.Diagnostics.AddError( "Unable create organization child", @@ -64,10 +67,8 @@ func (r *organizationResource) Create(ctx context.Context, req resource.CreateRe ) return } - data.Canonical = types.StringPointerValue(co.Canonical) - data.Name = types.StringPointerValue(co.Name) - data.Data = resource_organization.NewDataValueNull() - data.OrganizationCanonical = r.provider.OrganizationCanonical + + organizationCYModelToData(orgCan, co, &data) // Save data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) @@ -86,7 +87,19 @@ func (r *organizationResource) Read(ctx context.Context, req resource.ReadReques // Read API call logic api := common.NewAPI(common.WithURL(r.provider.Url.ValueString()), common.WithToken(r.provider.Jwt.ValueString())) mid := middleware.NewMiddleware(api) - org, err := mid.GetOrganization(data.Canonical.ValueString()) + + can := data.Canonical.ValueString() + if can == "" { + var plandata organizationResourceModel + // Read Terraform prior state data into the model + resp.Diagnostics.Append(req.State.Get(ctx, &plandata)...) + if resp.Diagnostics.HasError() { + return + } + can = plandata.Canonical.ValueString() + } + + org, err := mid.GetOrganization(can) if err != nil { resp.Diagnostics.AddError( "Unable get organization", @@ -95,7 +108,7 @@ func (r *organizationResource) Read(ctx context.Context, req resource.ReadReques return } - data.Name = types.StringPointerValue(org.Name) + organizationCYModelToData(can, org, &data) // Save updated data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) @@ -112,6 +125,33 @@ func (r *organizationResource) Update(ctx context.Context, req resource.UpdateRe } // Update API call logic + api := common.NewAPI(common.WithURL(r.provider.Url.ValueString()), common.WithToken(r.provider.Jwt.ValueString())) + mid := middleware.NewMiddleware(api) + can := data.Canonical.ValueString() + + // As the canonical is not required to be set we read it from the + // state as we set it on creation and we need it to update the + // credential to the API + if can == "" { + var plandata organizationResourceModel + // Read Terraform prior state data into the model + resp.Diagnostics.Append(req.State.Get(ctx, &plandata)...) + if resp.Diagnostics.HasError() { + return + } + can = plandata.Canonical.ValueString() + } + + uo, err := mid.UpdateOrganization(can, data.Name.ValueString()) + if err != nil { + resp.Diagnostics.AddError( + "Unable create organization child", + err.Error(), + ) + return + } + + organizationCYModelToData(can, uo, &data) // Save updated data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) @@ -139,3 +179,10 @@ func (r *organizationResource) Delete(ctx context.Context, req resource.DeleteRe return } } + +func organizationCYModelToData(org string, o *models.Organization, data *organizationResourceModel) { + data.Canonical = types.StringPointerValue(o.Canonical) + data.Name = types.StringPointerValue(o.Name) + data.Data = resource_organization.NewDataValueNull() + data.OrganizationCanonical = types.StringValue(org) +} diff --git a/provider/utils.go b/provider/utils.go new file mode 100644 index 0000000..30c4825 --- /dev/null +++ b/provider/utils.go @@ -0,0 +1,15 @@ +package provider + +import ( + "github.com/cycloidio/terraform-provider-cycloid/provider_cycloid" + "github.com/hashicorp/terraform-plugin-framework/types" +) + +func getOrganizationCanonical(pv provider_cycloid.CycloidModel, dataOrgCan types.String) string { + orgCan := pv.OrganizationCanonical.ValueString() + + if dOrgCan := dataOrgCan.ValueString(); dOrgCan != "" { + orgCan = dOrgCan + } + return orgCan +}