-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: csp tag * feat: wip csp tags * feat: csp tag datasource * fix: csp_tag examples * fix: lint errors
- Loading branch information
1 parent
92bc3af
commit 4fa10a2
Showing
11 changed files
with
628 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
#}, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.