Skip to content

Commit

Permalink
feat: tag assign csp (#581)
Browse files Browse the repository at this point in the history
* feat: csp tag

* feat: wip csp tags

* feat: csp tag datasource

* fix: csp_tag examples

* fix: lint errors
  • Loading branch information
wai-wong-edb authored Oct 8, 2024
1 parent 92bc3af commit 4fa10a2
Show file tree
Hide file tree
Showing 11 changed files with 628 additions and 24 deletions.
17 changes: 17 additions & 0 deletions examples/data-sources/biganimal_csp_tag/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
terraform {
required_providers {
biganimal = {
source = "EnterpriseDB/biganimal"
version = "1.0.0"
}
}
}

data "biganimal_csp_tag" "this" {
project_id = "<ex-project-id>" # ex: "prj_12345"
cloud_provider_id = "<ex-cloud-provider-id>" # ex: "aws"
}

output "csp_tags" {
value = data.biganimal_csp_tag.this.csp_tags
}
3 changes: 3 additions & 0 deletions examples/resources/biganimal_csp_tag/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# terraform import biganimal_cluster.<resource_name> <project_id>/<cloud_provider_id>
# example cloud provider id values bah:aws, bah:azure, bah:gcp, aws, azure, gcp
terraform import biganimal_csp_tag.this prj_deadbeef01234567/aws
48 changes: 48 additions & 0 deletions examples/resources/biganimal_csp_tag/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
terraform {
required_providers {
biganimal = {
source = "EnterpriseDB/biganimal"
version = "1.0.0"
}
random = {
source = "hashicorp/random"
version = "3.6.0"
}
}
}

resource "biganimal_csp_tag" "this" {
project_id = "<ex-project-id>" # ex: "prj_12345"
cloud_provider_id = "<ex-cloud-provider-id>" # ex cloud-provider-id values ["bah:aws", "bah:azure", "bah:gcp", "aws", "azure", "gcp"]

add_tags = [
#{
# csp_tag_key = "<ex-csp-tag-key>" # ex: "key"
# csp_tag_value = "<ex-csp-tag-value>" # ex: "value"
#},
#{
# csp_tag_key = "<ex-csp-tag-key>"
# csp_tag_value = "<ex-csp-tag-value>"
#},
]

delete_tags = [
#"<ex-csp-tag-id>", # ex: "id"
#"<ex-csp-tag-id>",
]

edit_tags = [
#{
# csp_tag_id = "<ex-csp-tag-id>" # ex: "id"
# csp_tag_key = "<ex-csp-tag-key>" # ex: "key"
# csp_tag_value = "<ex-csp-tag-value>" # ex: "value"
# status = "OK"
#},
#{
# csp_tag_id = "<ex-csp-tag-id>"
# csp_tag_key = "<ex-csp-tag-key>"
# csp_tag_value = "<ex-csp-tag-value>"
# status = "OK"
#},
]
}
5 changes: 5 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func (api *API) TagClient() *TagClient {
return c
}

func (api *API) CSPTagClient() *CSPTagClient {
c := NewCSPTagClient(*api)
return c
}

func BuildAPI(meta any) *API {
api, ok := meta.(*API)
if !ok {
Expand Down
67 changes: 67 additions & 0 deletions pkg/api/tag_csp_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package api

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/EnterpriseDB/terraform-provider-biganimal/pkg/models/common/api"
)

type CSPTagClient struct {
API
}

func NewCSPTagClient(api API) *CSPTagClient {
httpClient := http.Client{
Timeout: 60 * time.Second,
}

api.HTTPClient = httpClient
tc := CSPTagClient{API: api}
return &tc
}

func (c CSPTagClient) Put(ctx context.Context, projectID, cloudProviderID string, cspTagReq api.CSPTagRequest) (bool, error) {
b, err := json.Marshal(cspTagReq)
if err != nil {
return false, err
}

url := fmt.Sprintf("projects/%s/cloud-providers/%s/tags", projectID, cloudProviderID)
_, err = c.doRequest(ctx, http.MethodPut, url, bytes.NewBuffer(b))
if err != nil {
return false, err
}

return true, nil
}

func (c CSPTagClient) Get(ctx context.Context, projectID, cloudProviderID string) (*api.CSPTagResponse, error) {
response := &api.CSPTagResponse{}

url := fmt.Sprintf("projects/%s/cloud-providers/%s/tags", projectID, cloudProviderID)

body, err := c.doRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}

err = json.Unmarshal(body, &response)

return response, err
}

func (tc CSPTagClient) Delete(ctx context.Context, tagId string) error {
url := fmt.Sprintf("tags/%s", tagId)

_, err := tc.doRequest(ctx, http.MethodDelete, url, nil)
if err != nil {
return err
}

return nil
}
19 changes: 19 additions & 0 deletions pkg/models/common/api/csp_tag_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package api

type CSPTagRequest struct {
AddTags []AddTag `json:"addTags"`
DeleteTags []string `json:"deleteTags"`
EditTags []EditTag `json:"editTags"`
}

type AddTag struct {
CspTagKey string `json:"cspTagKey"`
CspTagValue string `json:"cspTagValue"`
}

type EditTag struct {
CSPTagID string `json:"cspTagId"`
CSPTagKey string `json:"cspTagKey"`
CSPTagValue string `json:"cspTagValue"`
Status string `json:"status"`
}
10 changes: 10 additions & 0 deletions pkg/models/common/api/csp_tag_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package api

type CSPTagResponse struct {
Data []struct {
CSPTagID string `json:"cspTagId"`
CSPTagKey string `json:"cspTagKey"`
CSPTagValue string `json:"cspTagValue"`
Status string `json:"status"`
} `json:"data"`
}
24 changes: 0 additions & 24 deletions pkg/provider/data_source_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,10 @@ var (
_ datasource.DataSourceWithConfigure = &clusterDataSource{}
)

type PgConfigDatasourceModel struct {
Value types.String `tfsdk:"value"`
Name types.String `tfsdk:"name"`
}

type StorageDatasourceModel struct {
Throughput types.String `tfsdk:"throughput"`
VolumeProperties types.String `tfsdk:"volume_properties"`
VolumeType types.String `tfsdk:"volume_type"`
Iops types.String `tfsdk:"iops"`
Size types.String `tfsdk:"size"`
}

type ClusterArchitectureDatasourceModel struct {
Nodes types.Int64 `tfsdk:"nodes"`
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
}

type clusterDatasourceModel struct {
ClusterResourceModel
}

type AllowedIpRangesDatasourceModel struct {
CidrBlock types.String `tfsdk:"cidr_block"`
Description types.String `tfsdk:"description"`
}

type clusterDataSource struct {
client *api.ClusterClient
}
Expand Down
138 changes: 138 additions & 0 deletions pkg/provider/data_source_csp_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package provider

import (
"context"

"github.com/EnterpriseDB/terraform-provider-biganimal/pkg/api"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var (
_ datasource.DataSource = &cSPTagDataSource{}
_ datasource.DataSourceWithConfigure = &cSPTagDataSource{}
)

type cSPTagDatasourceModel struct {
CSPTagResourceModel
}

type cSPTagDataSource struct {
client *api.CSPTagClient
}

func (c *cSPTagDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_csp_tag"
}

// Configure adds the provider configured client to the data source.
func (c *cSPTagDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

c.client = req.ProviderData.(*api.API).CSPTagClient()
}

func (c *cSPTagDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "CSP Tags will enable users to categorize and organize resources across types and improve the efficiency of resource retrieval",
// using Blocks for backward compatible
Blocks: map[string]schema.Block{
"timeouts": timeouts.Block(ctx,
timeouts.Opts{Create: true, Delete: true, Update: true},
),
},
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"project_id": schema.StringAttribute{
Required: true,
},
"cloud_provider_id": schema.StringAttribute{
Required: true,
},
"add_tags": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"csp_tag_key": schema.StringAttribute{
Computed: true,
},
"csp_tag_value": schema.StringAttribute{
Computed: true,
},
},
},
},
"delete_tags": schema.ListAttribute{
Computed: true,
ElementType: types.StringType,
},
"edit_tags": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"csp_tag_id": schema.StringAttribute{
Computed: true,
},
"csp_tag_key": schema.StringAttribute{
Computed: true,
},
"csp_tag_value": schema.StringAttribute{
Computed: true,
},
"status": schema.StringAttribute{
Computed: true,
},
},
},
},
"csp_tags": schema.ListNestedAttribute{
Description: "CSP Tags on cluster",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"csp_tag_id": schema.StringAttribute{
Computed: true,
},
"csp_tag_key": schema.StringAttribute{
Computed: true,
},
"csp_tag_value": schema.StringAttribute{
Computed: true,
},
"status": schema.StringAttribute{
Computed: true,
},
},
},
},
},
}
}

func (c *cSPTagDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data cSPTagDatasourceModel
diags := req.Config.Get(ctx, &data.CSPTagResourceModel)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

if err := readCSPTag(ctx, c.client, &data.CSPTagResourceModel); err != nil {
if !appendDiagFromBAErr(err, &resp.Diagnostics) {
resp.Diagnostics.AddError("Error reading cluster", err.Error())
}
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, data.CSPTagResourceModel)...)
}

func NewCSPTagDataSource() datasource.DataSource {
return &cSPTagDataSource{}
}
2 changes: 2 additions & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func (b bigAnimalProvider) DataSources(ctx context.Context) []func() datasource.
NewRegionsDataSource,
NewAnalyticsClusterDataSource,
NewTagDataSource,
NewCSPTagDataSource,
}
}

Expand All @@ -205,5 +206,6 @@ func (b bigAnimalProvider) Resources(ctx context.Context) []func() resource.Reso
NewFAReplicaResource,
NewAnalyticsClusterResource,
NewTagResource,
NewCSPTagResource,
}
}
Loading

0 comments on commit 4fa10a2

Please sign in to comment.