Skip to content

Commit

Permalink
Merge pull request #58 from couchbasecloud/AV-65076-main-branch-fixed
Browse files Browse the repository at this point in the history
Fix added for various resources creation failing in main branch
  • Loading branch information
nidhi07kumar authored Oct 18, 2023
2 parents 56a6f71 + 16f474e commit 48a869e
Show file tree
Hide file tree
Showing 18 changed files with 103 additions and 43 deletions.
2 changes: 1 addition & 1 deletion examples/demo/allowlist.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "capella_allowlist" "new_allowlist" {
organization_id = data.capella_organization.existing_organization.id
organization_id = var.organization_id
project_id = capella_project.new_project.id
cluster_id = capella_cluster.new_cluster.id
cidr = var.cidr
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/bucket.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ output "bucket" {

resource "capella_bucket" "new_bucket" {
name = var.bucket.name
organization_id = data.capella_organization.existing_organization.id
organization_id = var.organization_id
project_id = capella_project.new_project.id
cluster_id = capella_cluster.new_cluster.id
type = var.bucket.type
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/certificate.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ output "certificate" {
}

data "capella_certificate" "existing_certificate" {
organization_id = data.capella_organization.existing_organization.id
organization_id = var.organization_id
project_id = capella_project.new_project.id
cluster_id = capella_cluster.new_cluster.id
}
2 changes: 1 addition & 1 deletion examples/demo/cluster.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ output "cluster" {
}

resource "capella_cluster" "new_cluster" {
organization_id = data.capella_organization.existing_organization.id
organization_id = var.organization_id
project_id = capella_project.new_project.id
name = var.cluster.name
description = "My first test cluster for multiple services."
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/database_credential.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ output "database_credential" {

resource "capella_database_credential" "new_database_credential" {
name = var.database_credential_name
organization_id = data.capella_organization.existing_organization.id
organization_id = var.organization_id
project_id = capella_project.new_project.id
cluster_id = capella_cluster.new_cluster.id
password = var.password
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/terraform.template.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cloud_provider = {

cluster = {
name = "My First Terraform Cluster"
cidr = "192.168.0.0/20"
cidr = "10.0.30.0/23"
node_count = 3
server_version = "7.1"
couchbase_services = ["data", "index", "query"]
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/user.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ output "user" {
}

resource "capella_user" "new_user" {
organization_id = data.capella_organization.existing_organization.id
organization_id = var.organization_id

name = var.user.name
email = var.user.email
Expand Down
11 changes: 10 additions & 1 deletion internal/datasources/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ func (c *Certificate) Schema(_ context.Context, _ datasource.SchemaRequest, resp
"organization_id": requiredStringAttribute,
"project_id": requiredStringAttribute,
"cluster_id": requiredStringAttribute,
"certificate": computedStringAttribute,
"data": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"certificate": schema.StringAttribute{
Computed: true,
},
},
},
},
},
}
}
Expand Down
1 change: 0 additions & 1 deletion internal/resources/allowlist_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func AllowlistsSchema() schema.Schema {
"cidr": stringAttribute(required, requiresReplace),
"comment": stringAttribute(optional, requiresReplace),
"expires_at": stringAttribute(optional, requiresReplace),
"if_match": stringAttribute(optional),
"audit": computedAuditAttribute(),
},
}
Expand Down
5 changes: 2 additions & 3 deletions internal/resources/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ func (a *ApiKey) Create(ctx context.Context, req resource.CreateRequest, resp *r
}

refreshedState.Token = types.StringValue(apiKeyResponse.Token)
refreshedState.Rotate = plan.Rotate

// Set state to fully populated data
diags = resp.State.Set(ctx, refreshedState)
Expand Down Expand Up @@ -374,8 +373,8 @@ func (a *ApiKey) Update(ctx context.Context, req resource.UpdateRequest, resp *r
}

currentState.Secret = types.StringValue(rotateApiKeyResponse.SecretKey)
if !state.Id.IsNull() && !state.Id.IsUnknown() && !state.Secret.IsNull() && !state.Secret.IsUnknown() {
currentState.Token = types.StringValue(base64.StdEncoding.EncodeToString([]byte(state.Id.ValueString() + ":" + state.Secret.ValueString())))
if !currentState.Id.IsNull() && !currentState.Id.IsUnknown() && !currentState.Secret.IsNull() && !currentState.Secret.IsUnknown() {
currentState.Token = types.StringValue(base64.StdEncoding.EncodeToString([]byte(currentState.Id.ValueString() + ":" + currentState.Secret.ValueString())))
}
currentState.Rotate = plan.Rotate

Expand Down
26 changes: 13 additions & 13 deletions internal/resources/bucket_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ func BucketSchema() schema.Schema {
stringplanmodifier.UseStateForUnknown(),
},
},
"organization_id": stringAttribute(required),
"project_id": stringAttribute(required),
"cluster_id": stringAttribute(required),
"name": stringAttribute(required),
"type": stringAttribute(optional),
"storage_backend": stringAttribute(optional),
"memory_allocationinmb": int64Attribute(optional),
"conflict_resolution": stringAttribute(optional),
"durability_level": stringAttribute(optional),
"replicas": int64Attribute(optional),
"flush": boolAttribute(optional),
"ttl": int64Attribute(optional),
"eviction_policy": stringAttribute(computed, optional),
"organization_id": stringAttribute(required),
"project_id": stringAttribute(required),
"cluster_id": stringAttribute(required),
"name": stringAttribute(required),
"type": stringAttribute(optional),
"storage_backend": stringAttribute(optional),
"memory_allocation_in_mb": int64Attribute(optional),
"bucket_conflict_resolution": stringAttribute(optional),
"durability_level": stringAttribute(optional),
"replicas": int64Attribute(optional),
"flush": boolAttribute(optional),
"time_to_live_in_seconds": int64Attribute(optional),
"eviction_policy": stringAttribute(computed, optional),
"stats": schema.SingleNestedAttribute{
Computed: true,
Attributes: map[string]schema.Attribute{
Expand Down
3 changes: 1 addition & 2 deletions internal/resources/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import (

"terraform-provider-capella/internal/api"
clusterapi "terraform-provider-capella/internal/api/cluster"
providerschema "terraform-provider-capella/internal/schema"

"terraform-provider-capella/internal/errors"
providerschema "terraform-provider-capella/internal/schema"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down
4 changes: 4 additions & 0 deletions internal/resources/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ func (r *Project) Read(ctx context.Context, req resource.ReadRequest, resp *reso
return
}

if !state.IfMatch.IsUnknown() && !state.IfMatch.IsNull() {
refreshedState.IfMatch = state.IfMatch
}

// Set refreshed state
diags = resp.State.Set(ctx, &refreshedState)
resp.Diagnostics.Append(diags...)
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/project_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func ProjectSchema() schema.Schema {
},
"organization_id": stringAttribute(required),
"name": stringAttribute(required),
"description": stringAttribute(optional),
"description": stringAttribute(optional, computed),
"if_match": stringAttribute(optional),
"etag": stringAttribute(computed),
"audit": computedAuditAttribute(),
Expand Down
9 changes: 5 additions & 4 deletions internal/resources/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"

"terraform-provider-capella/internal/api"
"terraform-provider-capella/internal/errors"
providerschema "terraform-provider-capella/internal/schema"
Expand Down Expand Up @@ -117,7 +118,7 @@ func (r *User) Create(ctx context.Context, req resource.CreateRequest, resp *res
return
}

refreshedState, err := r.refreshUser(ctx, organizationId, createUserResponse.Id.String())
refreshedState, err := r.refreshUser(ctx, organizationId, createUserResponse.Id.String(), plan)
if err != nil {
resp.Diagnostics.AddError(
"Error reading user",
Expand Down Expand Up @@ -174,7 +175,7 @@ func (r *User) Read(ctx context.Context, req resource.ReadRequest, resp *resourc
)

// Refresh the existing user
refreshedState, err := r.refreshUser(ctx, organizationId, userId)
refreshedState, err := r.refreshUser(ctx, organizationId, userId, state)
switch err := err.(type) {
case nil:
case api.Error:
Expand Down Expand Up @@ -295,7 +296,7 @@ func (r *User) getUser(ctx context.Context, organizationId, userId string) (*api
return &userResp, nil
}

func (r *User) refreshUser(ctx context.Context, organizationId, userId string) (*providerschema.User, error) {
func (r *User) refreshUser(ctx context.Context, organizationId, userId string, plan providerschema.User) (*providerschema.User, error) {
userResp, err := r.getUser(ctx, organizationId, userId)
if err != nil {
return nil, handleCapellaUserError(err)
Expand Down Expand Up @@ -326,7 +327,7 @@ func (r *User) refreshUser(ctx context.Context, organizationId, userId string) (
types.StringValue(userResp.TimeZone),
types.BoolValue(userResp.EnableNotifications),
types.StringValue(userResp.ExpiresAt),
providerschema.MorphResources(userResp.Resources),
plan.Resources,
auditObj,
)
return refreshedState, nil
Expand Down
4 changes: 0 additions & 4 deletions internal/schema/allowlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ type AllowList struct {
// ClusterId is the clusterId of the capella tenant.
ClusterId types.String `tfsdk:"cluster_id"`

IfMatch types.String `tfsdk:"if_match"`

// Audit represents all audit-related fields. It is of types.Object type to avoid conversion error for a nested field.
Audit types.Object `tfsdk:"audit"`
}
Expand Down Expand Up @@ -155,6 +153,4 @@ type OneAllowList struct {

// ClusterId is the clusterId of the capella tenant.
ClusterId types.String `tfsdk:"cluster_id"`

IfMatch types.String `tfsdk:"if_match"`
}
2 changes: 2 additions & 0 deletions internal/schema/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type ApiKey struct {
Token types.String `tfsdk:"token"`
}

// NewApiKey creates new apikey object
func NewApiKey(apiKey *api.GetApiKeyResponse, organizationId string, auditObject basetypes.ObjectValue) (*ApiKey, error) {
newApiKey := ApiKey{
Id: types.StringValue(apiKey.Id),
Expand Down Expand Up @@ -246,6 +247,7 @@ type ApiKeyData struct {
Resources []ApiKeyResourcesItems `tfsdk:"resources"`
}

// NewApiKeyData creates a new apiKeyData object
func NewApiKeyData(apiKey *api.GetApiKeyResponse, organizationId string, auditObject basetypes.ObjectValue) (ApiKeyData, error) {
newApiKeyData := ApiKeyData{
Id: types.StringValue(apiKey.Id),
Expand Down
65 changes: 58 additions & 7 deletions internal/schema/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,72 @@ type Support struct {

// Cluster defines the response as received from V4 Capella Public API when asked to create a new cluster.
type Cluster struct {
ClusterData
Id types.String `tfsdk:"id"`

// AppServiceId is the ID of the linked app service.
AppServiceId types.String `tfsdk:"app_service_id"`
Audit types.Object `tfsdk:"audit"`
OrganizationId types.String `tfsdk:"organization_id"`
ProjectId types.String `tfsdk:"project_id"`
Availability *Availability `tfsdk:"availability"`

// CloudProvider The cloud provider where the cluster will be hosted.
// To learn more, see [Amazon Web Services](https://docs.couchbase.com/cloud/reference/aws.html).
CloudProvider *CloudProvider `tfsdk:"cloud_provider"`
CouchbaseServer *CouchbaseServer `tfsdk:"couchbase_server"`

// Description of the cluster (up to 1024 characters).
Description types.String `tfsdk:"description"`

Etag types.String `tfsdk:"etag"`
// Name of the cluster (up to 256 characters).
Name types.String `tfsdk:"name"`

// ServiceGroups is the couchbase service groups to be run. At least one service group must contain the data service.
ServiceGroups []ServiceGroup `tfsdk:"service_groups"`
Support *Support `tfsdk:"support"`
CurrentState types.String `tfsdk:"current_state"`
Etag types.String `tfsdk:"etag"`

IfMatch types.String `tfsdk:"if_match"`
}

// NewCluster create new cluster object
func NewCluster(cluster *clusterapi.GetClusterResponse, organizationId, projectId string, auditObject basetypes.ObjectValue) (*Cluster, error) {
newClusterData, err := NewClusterData(cluster, organizationId, projectId, auditObject)
newCluster := Cluster{
Id: types.StringValue(cluster.Id.String()),
OrganizationId: types.StringValue(organizationId),
ProjectId: types.StringValue(projectId),
Name: types.StringValue(cluster.Name),
Description: types.StringValue(cluster.Description),
Availability: &Availability{
Type: types.StringValue(string(cluster.Availability.Type)),
},
CloudProvider: &CloudProvider{
Cidr: types.StringValue(cluster.CloudProvider.Cidr),
Region: types.StringValue(cluster.CloudProvider.Region),
Type: types.StringValue(string(cluster.CloudProvider.Type)),
},
Support: &Support{
Plan: types.StringValue(string(cluster.Support.Plan)),
Timezone: types.StringValue(string(cluster.Support.Timezone)),
},
CurrentState: types.StringValue(string(cluster.CurrentState)),
Audit: auditObject,
Etag: types.StringValue(cluster.Etag),
}

if cluster.CouchbaseServer.Version != nil {
version := *cluster.CouchbaseServer.Version
newCluster.CouchbaseServer = &CouchbaseServer{
Version: types.StringValue(version),
}
}

newServiceGroups, err := morphToTerraformServiceGroups(cluster)
if err != nil {
return nil, err
}
newCluster := Cluster{
ClusterData: *newClusterData,
Etag: types.StringValue(cluster.Etag),
}
newCluster.ServiceGroups = newServiceGroups
return &newCluster, nil
}

Expand Down Expand Up @@ -307,6 +357,7 @@ type ClusterData struct {
CurrentState types.String `tfsdk:"current_state"`
}

// NewClusterData creates a new cluster data object
func NewClusterData(cluster *clusterapi.GetClusterResponse, organizationId, projectId string, auditObject basetypes.ObjectValue) (*ClusterData, error) {
newClusterData := ClusterData{
Id: types.StringValue(cluster.Id.String()),
Expand Down

0 comments on commit 48a869e

Please sign in to comment.