Skip to content

Commit

Permalink
provider/organization: Added full Organizaiton CRUD
Browse files Browse the repository at this point in the history
And also enabled the 'organization_canonical' on any resource to point to the org in which it should
be created to instead of the default one defined on the provider
  • Loading branch information
xescugc committed Mar 14, 2024
1 parent e39bab1 commit ae73316
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.tf
*.tfstate*
main.tf
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
28 changes: 0 additions & 28 deletions main.tf

This file was deleted.

25 changes: 17 additions & 8 deletions provider/credential_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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",
Expand All @@ -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)...)
Expand All @@ -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",
Expand All @@ -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)...)
Expand Down Expand Up @@ -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",
Expand All @@ -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)...)
Expand All @@ -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",
Expand Down
61 changes: 54 additions & 7 deletions provider/organization_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -56,18 +57,18 @@ 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",
err.Error(),
)
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)...)
Expand All @@ -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",
Expand All @@ -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)...)
Expand All @@ -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)...)
Expand Down Expand Up @@ -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)
}
15 changes: 15 additions & 0 deletions provider/utils.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ae73316

Please sign in to comment.