Skip to content

Commit

Permalink
Refactor cyral_sidecar_credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
wcmjunior committed Apr 2, 2024
1 parent 1cc3531 commit 7fc290f
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 130 deletions.
5 changes: 5 additions & 0 deletions cyral/internal/sidecar/credentials/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package credentials

const (
resourceName = "cyral_sidecar_credentials"
)
38 changes: 38 additions & 0 deletions cyral/internal/sidecar/credentials/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package credentials

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

type CreateSidecarCredentialsRequest struct {
SidecarID string `json:"sidecarId"`
}

func (r *CreateSidecarCredentialsRequest) ReadFromSchema(d *schema.ResourceData) error {
r.SidecarID = d.Get("sidecar_id").(string)
return nil
}

type SidecarCredentialsData struct {
SidecarID string `json:"sidecarId"`
ClientID string `json:"clientId"`
ClientSecret string `json:"clientSecret"`
}

func (r *SidecarCredentialsData) WriteToSchema(d *schema.ResourceData) error {
if err := d.Set("client_id", r.ClientID); err != nil {
return fmt.Errorf("error setting 'client_id' field: %w", err)
}
if r.ClientSecret != "" {
if err := d.Set("client_secret", r.ClientSecret); err != nil {
return fmt.Errorf("error setting 'client_secret' field: %w", err)
}
}
if err := d.Set("sidecar_id", r.SidecarID); err != nil {
return fmt.Errorf("error setting 'sidecar_id' field: %w", err)
}
d.SetId(r.ClientID)
return nil
}
58 changes: 58 additions & 0 deletions cyral/internal/sidecar/credentials/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package credentials

import (
"fmt"

"github.com/cyralinc/terraform-provider-cyral/cyral/client"
"github.com/cyralinc/terraform-provider-cyral/cyral/core"
"github.com/cyralinc/terraform-provider-cyral/cyral/core/types/resourcetype"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var resourceContextHandler = core.DefaultContextHandler{
ResourceName: resourceName,
ResourceType: resourcetype.Resource,
SchemaReaderFactory: func() core.SchemaReader { return &CreateSidecarCredentialsRequest{} },
SchemaWriterFactoryGetMethod: func(_ *schema.ResourceData) core.SchemaWriter { return &SidecarCredentialsData{} },
SchemaWriterFactoryPostMethod: func(_ *schema.ResourceData) core.SchemaWriter { return &SidecarCredentialsData{} },
BaseURLFactory: func(d *schema.ResourceData, c *client.Client) string {
return fmt.Sprintf("https://%s/v1/users/sidecarAccounts", c.ControlPlane)
},
}

func resourceSchema() *schema.Resource {
return &schema.Resource{
Description: "Create new [credentials for Cyral sidecar](https://cyral.com/docs/sidecars/sidecar-manage/#rotate-the-client-secret-for-a-sidecar).",
CreateContext: resourceContextHandler.CreateContext(),
ReadContext: resourceContextHandler.ReadContext(),
DeleteContext: resourceContextHandler.DeleteContext(),

Schema: map[string]*schema.Schema{
"id": {
Description: "Same as `client_id`.",
Type: schema.TypeString,
Computed: true,
},
"sidecar_id": {
Description: "ID of the sidecar to create new credentials.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"client_id": {
Description: "Sidecar client ID.",
Type: schema.TypeString,
Computed: true,
},
"client_secret": {
Description: "Sidecar client secret.",
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}

This file was deleted.

26 changes: 26 additions & 0 deletions cyral/internal/sidecar/credentials/schema_loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package credentials

import (
"github.com/cyralinc/terraform-provider-cyral/cyral/core"
)

type packageSchema struct {
}

func (p *packageSchema) Name() string {
return "credentials"
}

func (p *packageSchema) Schemas() []*core.SchemaDescriptor {
return []*core.SchemaDescriptor{
{
Name: resourceName,
Type: core.ResourceSchemaType,
Schema: resourceSchema,
},
}
}

func PackageSchema() core.PackageSchema {
return &packageSchema{}
}
2 changes: 0 additions & 2 deletions cyral/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/cyralinc/terraform-provider-cyral/cyral/internal/samlconfiguration"
"github.com/cyralinc/terraform-provider-cyral/cyral/internal/serviceaccount"
"github.com/cyralinc/terraform-provider-cyral/cyral/internal/sidecar"
"github.com/cyralinc/terraform-provider-cyral/cyral/internal/sidecar/credentials"
"github.com/cyralinc/terraform-provider-cyral/cyral/internal/sidecar/health"
"github.com/cyralinc/terraform-provider-cyral/cyral/internal/sidecar/instance"
"github.com/cyralinc/terraform-provider-cyral/cyral/internal/sidecar/listener"
Expand Down Expand Up @@ -169,7 +168,6 @@ func getResourceMap(ps []core.PackageSchema) map[string]*schema.Resource {
schemaMap["cyral_role"] = role.ResourceRole()
schemaMap["cyral_role_sso_groups"] = role.ResourceRoleSSOGroups()
schemaMap["cyral_service_account"] = serviceaccount.ResourceServiceAccount()
schemaMap["cyral_sidecar_credentials"] = credentials.ResourceSidecarCredentials()
schemaMap["cyral_sidecar_listener"] = listener.ResourceSidecarListener()

tflog.Debug(ctx, "End getResourceMap")
Expand Down
2 changes: 2 additions & 0 deletions cyral/provider/schema_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/cyralinc/terraform-provider-cyral/cyral/internal/repository/useraccount"
"github.com/cyralinc/terraform-provider-cyral/cyral/internal/samlcertificate"
"github.com/cyralinc/terraform-provider-cyral/cyral/internal/sidecar"
"github.com/cyralinc/terraform-provider-cyral/cyral/internal/sidecar/credentials"
"github.com/cyralinc/terraform-provider-cyral/cyral/internal/tokensettings"
)

Expand All @@ -22,6 +23,7 @@ func packagesSchemas() []core.PackageSchema {
accessgateway.PackageSchema(),
accessrules.PackageSchema(),
binding.PackageSchema(),
credentials.PackageSchema(),
datalabel.PackageSchema(),
datamap.PackageSchema(),
hcvault.PackageSchema(),
Expand Down

0 comments on commit 7fc290f

Please sign in to comment.