Skip to content

Commit

Permalink
feat: tags assign to cluster resources single/ha/faraway-replica (#526)
Browse files Browse the repository at this point in the history
* feat: resource tag create

* fix: lint errors

* fix: lint errors

* refactor: refactor tag client

* feat: tag update

* refactor: refactor tag client

* feat: changed tag description

* feat: tag import

* feat: lint error

* feat: assign tags to resources

* fix: faraway replica assign tag

* feat: tag in cluster examples

* fix: lint fixes

* feat: faraway-replica tag fix

* fix: faraway-replica data source
  • Loading branch information
wai-wong-edb authored Aug 16, 2024
1 parent 0e10501 commit d74f2ce
Show file tree
Hide file tree
Showing 14 changed files with 230 additions and 8 deletions.
10 changes: 10 additions & 0 deletions examples/resources/biganimal_cluster/ha/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ resource "biganimal_cluster" "ha_cluster" {
# ]
}

#tags = [
# {
# tag_name = "test-tag-1"
# color = "blue"
# },
# {
# tag_name = "test-tag-2"
# },
#]

# transparent_data_encryption = {
# key_id = <example_value>
# }
Expand Down
10 changes: 10 additions & 0 deletions examples/resources/biganimal_cluster/single_node/aws/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ resource "biganimal_cluster" "single_node_cluster" {
# ]
}

#tags = [
# {
# tag_name = "test-tag-1"
# color = "blue"
# },
# {
# tag_name = "test-tag-2"
# },
#]

# pe_allowed_principal_ids = [
# <example_value> # ex: 123456789012
# ]
Expand Down
10 changes: 10 additions & 0 deletions examples/resources/biganimal_cluster/single_node/azure/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ resource "biganimal_cluster" "single_node_cluster" {
# ]
}

#tags = [
# {
# tag_name = "test-tag-1"
# color = "blue"
# },
# {
# tag_name = "test-tag-2"
# },
#]

# pe_allowed_principal_ids = [
# <example_value> # ex: "9334e5e6-7f47-aE61-5A4F-ee067daeEf4A"
# ]
Expand Down
10 changes: 10 additions & 0 deletions examples/resources/biganimal_cluster/single_node/gcp/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ resource "biganimal_cluster" "single_node_cluster" {
# ]
}

#tags = [
# {
# tag_name = "test-tag-1"
# color = "blue"
# },
# {
# tag_name = "test-tag-2"
# },
#]

# pe_allowed_principal_ids = [
# <example_value> # ex: "development-data-123456"
# ]
Expand Down
13 changes: 12 additions & 1 deletion examples/resources/biganimal_faraway_replica/aws/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,18 @@ resource "biganimal_faraway_replica" "faraway_replica" {
size = "4 Gi"
}
private_networking = false
region = "eu-west-2"
region = "ap-south-1"

#tags = [
# {
# tag_name = "test-tag-1"
# color = "blue"
# },
# {
# tag_name = "test-tag-2"
# },
#]

# pe_allowed_principal_ids = [
# <example_value> # ex: 123456789012
# ]
Expand Down
12 changes: 11 additions & 1 deletion examples/resources/biganimal_faraway_replica/azure/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,19 @@ resource "biganimal_faraway_replica" "faraway_replica" {
volume_properties = "P1"
size = "4 Gi"
}

private_networking = false
region = "australiaeast"

#tags = [
# {
# tag_name = "test-tag-1"
# color = "blue"
# },
# {
# tag_name = "test-tag-2"
# },
#]

# pe_allowed_principal_ids = [
# <example_value> # ex: "9334e5e6-7f47-aE61-5A4F-ee067daeEf4A"
# ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ resource "biganimal_faraway_replica" "faraway_replica" {

backup_retention_period = "8d"
csp_auth = false
instance_type = "aws:c6i.large"
instance_type = "azure:Standard_D2s_v3"

// only following pg_config parameters are configurable for faraway replica
// max_connections, max_locks_per_transaction, max_prepared_transactions, max_wal_senders, max_worker_processes.
Expand All @@ -102,10 +102,19 @@ resource "biganimal_faraway_replica" "faraway_replica" {
volume_properties = "P1"
size = "4 Gi"
}

private_networking = false
region = "centralindia"

#tags = [
# {
# tag_name = "test-tag-1"
# color = "blue"
# },
# {
# tag_name = "test-tag-2"
# },
#]

# transparent_data_encryption = {
# key_id = <example_value>
# }
Expand Down
13 changes: 12 additions & 1 deletion examples/resources/biganimal_faraway_replica/gcp/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,21 @@ resource "biganimal_faraway_replica" "faraway_replica" {
storage = {
volume_type = "pd-ssd"
volume_properties = "pd-ssd"
size = "10 Gi"
size = "4 Gi"
}
private_networking = false
region = "us-east1"

#tags = [
# {
# tag_name = "test-tag-1"
# color = "blue"
# },
# {
# tag_name = "test-tag-2"
# },
#]

# pe_allowed_principal_ids = [
# <example_value> # ex: "development-data-123456"
# ]
Expand Down
2 changes: 2 additions & 0 deletions examples/resources/biganimal_faraway_replica/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# terraform import biganimal_faraway_replica.<resource_name> <project_id>/<cluster_id>
terraform import biganimal_faraway_replica.faraway_replica prj_deadbeef01234567/p-abcd123456
11 changes: 11 additions & 0 deletions pkg/models/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ func NewCluster(d *schema.ResourceData) (*Cluster, error) {
return nil, err
}

tagKey := d.Get("tags")
tags := []commonApi.Tag{}
if tagKey != nil {
tags, err = utils.StructFromProps[[]commonApi.Tag](tagKey.(*schema.Set).List())
if err != nil {
return nil, err
}
}

cluster := &Cluster{
ReplicaSourceClusterId: SourceId,
ClusterType: ClusterType,
Expand Down Expand Up @@ -96,6 +105,7 @@ func NewCluster(d *schema.ResourceData) (*Cluster, error) {
},
ReadOnlyConnections: clusterRoConn,
Storage: &storage,
Tags: tags,
}

return cluster, nil
Expand Down Expand Up @@ -171,6 +181,7 @@ type Cluster struct {
SuperuserAccess *bool `json:"superuserAccess,omitempty"`
Extensions *[]ClusterExtension `json:"extensions,omitempty"`
PgBouncer *PgBouncer `json:"pgBouncer,omitempty"`
Tags []commonApi.Tag `json:"tags,omitempty"`
VolumeSnapshot *bool `json:"volumeSnapshotBackup,omitempty"`
EncryptionKeyIdReq *string `json:"keyId,omitempty"`
EncryptionKeyResp *EncryptionKey `json:"encryptionKey,omitempty"`
Expand Down
8 changes: 8 additions & 0 deletions pkg/models/common/api/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package api

// mapstructure annotations are used by faraway away replica only and should be removed once we migrate faraway replica resouce to terraform plugin framework
type Tag struct {
Color *string `json:"color,omitempty" mapstructure:"color"`
TagId string `json:"tagId" mapstructure:"tag_id"`
TagName string `json:"tagName" mapstructure:"tag_name"`
}
17 changes: 17 additions & 0 deletions pkg/provider/data_source_fareplica.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,23 @@ func (c *FAReplicaData) Schema(ctx context.Context, req datasource.SchemaRequest
MarkdownDescription: "Transparent data encryption action.",
Computed: true,
},
"tags": schema.SetNestedAttribute{
Description: "Assign existing tags or create tags to assign to this resource",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"tag_id": schema.StringAttribute{
Computed: true,
},
"tag_name": schema.StringAttribute{
Required: true,
},
"color": schema.StringAttribute{
Computed: true,
},
},
},
},
},
}
}
Expand Down
50 changes: 50 additions & 0 deletions pkg/provider/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type ClusterResourceModel struct {
PgIdentity types.String `tfsdk:"pg_identity"`
TransparentDataEncryptionAction types.String `tfsdk:"transparent_data_encryption_action"`
VolumeSnapshot types.Bool `tfsdk:"volume_snapshot_backup"`
Tags []commonTerraform.Tag `tfsdk:"tags"`

Timeouts timeouts.Value `tfsdk:"timeouts"`
}
Expand Down Expand Up @@ -502,6 +503,36 @@ func (c *clusterResource) Schema(ctx context.Context, req resource.SchemaRequest
Optional: true,
PlanModifiers: []planmodifier.Bool{boolplanmodifier.UseStateForUnknown()},
},
"tags": schema.SetNestedAttribute{
Description: "Assign existing tags or create tags to assign to this resource",
Optional: true,
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"tag_id": schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"tag_name": schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"color": schema.StringAttribute{
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
},
PlanModifiers: []planmodifier.Set{
setplanmodifier.UseStateForUnknown(),
},
},
"transparent_data_encryption": schema.SingleNestedAttribute{
MarkdownDescription: "Transparent Data Encryption (TDE) key",
Optional: true,
Expand Down Expand Up @@ -932,6 +963,15 @@ func readCluster(ctx context.Context, client *api.ClusterClient, tfClusterResour
}
}

tfClusterResource.Tags = []commonTerraform.Tag{}
for _, v := range responseCluster.Tags {
tfClusterResource.Tags = append(tfClusterResource.Tags, commonTerraform.Tag{
TagId: types.StringValue(v.TagId),
TagName: types.StringValue(v.TagName),
Color: basetypes.NewStringPointerValue(v.Color),
})
}

if responseCluster.EncryptionKeyResp != nil {
tfClusterResource.TransparentDataEncryption = &TransparentDataEncryptionModel{}
tfClusterResource.TransparentDataEncryption.KeyId = types.StringValue(responseCluster.EncryptionKeyResp.KeyId)
Expand Down Expand Up @@ -1120,6 +1160,16 @@ func (c *clusterResource) generateGenericClusterModel(ctx context.Context, clust
}
}

tags := []commonApi.Tag{}
for _, tag := range clusterResource.Tags {
tags = append(tags, commonApi.Tag{
Color: tag.Color.ValueStringPointer(),
TagId: tag.TagId.ValueString(),
TagName: tag.TagName.ValueString(),
})
}
cluster.Tags = tags

svAccIds, principalIds, err := c.buildRequestBah(ctx, clusterResource)
if err != nil {
return models.Cluster{}, err
Expand Down
Loading

0 comments on commit d74f2ce

Please sign in to comment.