generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: added payment method resource and data endpoint #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7eef1cf
feat: added payment method resource and data endpoint
younGihan 27c1f76
style: formatting and generating additional comments
younGihan c29a058
Update client/payment_method.go
younGihan b61b799
feat: removed workspace identifier for URL creation as not required
younGihan 68f862a
fix: fixed the timestamp examples and added meaningful example for tags
younGihan b43896f
fix: executed go generate after minior changes on code and docs
younGihan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,169 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| ) | ||
|
|
||
| const CONTENT_TYPE_PAYMENT_METHOD = "application/vnd.meshcloud.api.meshpaymentmethod.v2.hal+json" | ||
|
|
||
| type MeshPaymentMethod struct { | ||
| ApiVersion string `json:"apiVersion" tfsdk:"api_version"` | ||
| Kind string `json:"kind" tfsdk:"kind"` | ||
| Metadata MeshPaymentMethodMetadata `json:"metadata" tfsdk:"metadata"` | ||
| Spec MeshPaymentMethodSpec `json:"spec" tfsdk:"spec"` | ||
| } | ||
|
|
||
| type MeshPaymentMethodMetadata struct { | ||
| Name string `json:"name" tfsdk:"name"` | ||
| OwnedByWorkspace string `json:"ownedByWorkspace" tfsdk:"owned_by_workspace"` | ||
| CreatedOn string `json:"createdOn" tfsdk:"created_on"` | ||
| DeletedOn *string `json:"deletedOn" tfsdk:"deleted_on"` | ||
| } | ||
|
|
||
| type MeshPaymentMethodSpec struct { | ||
| DisplayName string `json:"displayName" tfsdk:"display_name"` | ||
| ExpirationDate *string `json:"expirationDate,omitempty" tfsdk:"expiration_date"` | ||
| Amount *int64 `json:"amount,omitempty" tfsdk:"amount"` | ||
| Tags map[string][]string `json:"tags,omitempty" tfsdk:"tags"` | ||
| } | ||
|
|
||
| type MeshPaymentMethodCreate struct { | ||
| ApiVersion string `json:"apiVersion" tfsdk:"api_version"` | ||
| Metadata MeshPaymentMethodCreateMetadata `json:"metadata" tfsdk:"metadata"` | ||
| Spec MeshPaymentMethodSpec `json:"spec" tfsdk:"spec"` | ||
| } | ||
|
|
||
| type MeshPaymentMethodCreateMetadata struct { | ||
| Name string `json:"name" tfsdk:"name"` | ||
| OwnedByWorkspace string `json:"ownedByWorkspace" tfsdk:"owned_by_workspace"` | ||
| } | ||
|
|
||
| func (c *MeshStackProviderClient) urlForPaymentMethod(identifier string) *url.URL { | ||
| return c.endpoints.PaymentMethods.JoinPath(identifier) | ||
| } | ||
|
|
||
| func (c *MeshStackProviderClient) ReadPaymentMethod(workspace string, identifier string) (*MeshPaymentMethod, error) { | ||
| targetUrl := c.urlForPaymentMethod(identifier) | ||
|
|
||
| req, err := http.NewRequest("GET", targetUrl.String(), nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| req.Header.Set("Accept", CONTENT_TYPE_PAYMENT_METHOD) | ||
|
|
||
| res, err := c.doAuthenticatedRequest(req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| defer res.Body.Close() | ||
|
|
||
| if res.StatusCode == http.StatusNotFound { | ||
| return nil, nil | ||
| } | ||
|
|
||
| data, err := io.ReadAll(res.Body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if !isSuccessHTTPStatus(res) { | ||
| return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data) | ||
| } | ||
|
|
||
| var paymentMethod MeshPaymentMethod | ||
| err = json.Unmarshal(data, &paymentMethod) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &paymentMethod, nil | ||
| } | ||
|
|
||
| func (c *MeshStackProviderClient) CreatePaymentMethod(paymentMethod *MeshPaymentMethodCreate) (*MeshPaymentMethod, error) { | ||
| payload, err := json.Marshal(paymentMethod) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| req, err := http.NewRequest("POST", c.endpoints.PaymentMethods.String(), bytes.NewBuffer(payload)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| req.Header.Set("Content-Type", CONTENT_TYPE_PAYMENT_METHOD) | ||
| req.Header.Set("Accept", CONTENT_TYPE_PAYMENT_METHOD) | ||
|
|
||
| res, err := c.doAuthenticatedRequest(req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| defer res.Body.Close() | ||
|
|
||
| data, err := io.ReadAll(res.Body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if !isSuccessHTTPStatus(res) { | ||
| return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data) | ||
| } | ||
|
|
||
| var createdPaymentMethod MeshPaymentMethod | ||
| err = json.Unmarshal(data, &createdPaymentMethod) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &createdPaymentMethod, nil | ||
| } | ||
|
|
||
| func (c *MeshStackProviderClient) UpdatePaymentMethod(identifier string, paymentMethod *MeshPaymentMethodCreate) (*MeshPaymentMethod, error) { | ||
| targetUrl := c.urlForPaymentMethod(identifier) | ||
|
|
||
| payload, err := json.Marshal(paymentMethod) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| req, err := http.NewRequest("PUT", targetUrl.String(), bytes.NewBuffer(payload)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| req.Header.Set("Content-Type", CONTENT_TYPE_PAYMENT_METHOD) | ||
| req.Header.Set("Accept", CONTENT_TYPE_PAYMENT_METHOD) | ||
|
|
||
| res, err := c.doAuthenticatedRequest(req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| defer res.Body.Close() | ||
|
|
||
| data, err := io.ReadAll(res.Body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if !isSuccessHTTPStatus(res) { | ||
| return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data) | ||
| } | ||
|
|
||
| var updatedPaymentMethod MeshPaymentMethod | ||
| err = json.Unmarshal(data, &updatedPaymentMethod) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &updatedPaymentMethod, nil | ||
| } | ||
|
|
||
| func (c *MeshStackProviderClient) DeletePaymentMethod(identifier string) error { | ||
| targetUrl := c.urlForPaymentMethod(identifier) | ||
| return c.deleteMeshObject(*targetUrl, 204) | ||
| } | ||
This file contains hidden or 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,59 @@ | ||
| --- | ||
| # generated by https://github.com/hashicorp/terraform-plugin-docs | ||
| page_title: "meshstack_payment_method Data Source - terraform-provider-meshstack" | ||
| subcategory: "" | ||
| description: |- | ||
| Read a single payment method by workspace and identifier. | ||
| --- | ||
|
|
||
| # meshstack_payment_method (Data Source) | ||
|
|
||
| Read a single payment method by workspace and identifier. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ```terraform | ||
| data "meshstack_payment_method" "example" { | ||
| metadata = { | ||
| name = "my-payment-method" | ||
| owned_by_workspace = "my-workspace-identifier" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| <!-- schema generated by tfplugindocs --> | ||
| ## Schema | ||
|
|
||
| ### Required | ||
|
|
||
| - `metadata` (Attributes) (see [below for nested schema](#nestedatt--metadata)) | ||
|
|
||
| ### Read-Only | ||
|
|
||
| - `api_version` (String) Payment method API version. | ||
| - `kind` (String) meshObject type, always `meshPaymentMethod`. | ||
| - `spec` (Attributes) (see [below for nested schema](#nestedatt--spec)) | ||
|
|
||
| <a id="nestedatt--metadata"></a> | ||
| ### Nested Schema for `metadata` | ||
|
|
||
| Required: | ||
|
|
||
| - `name` (String) Payment method identifier. | ||
| - `owned_by_workspace` (String) Identifier of the workspace that owns this payment method. | ||
|
|
||
| Read-Only: | ||
|
|
||
| - `created_on` (String) Creation date of the payment method. | ||
| - `deleted_on` (String) Deletion date of the payment method. | ||
|
|
||
|
|
||
| <a id="nestedatt--spec"></a> | ||
| ### Nested Schema for `spec` | ||
|
|
||
| Read-Only: | ||
|
|
||
| - `amount` (Number) Amount associated with the payment method. | ||
| - `display_name` (String) Display name of the payment method. | ||
| - `expiration_date` (String) Expiration date of the payment method (ISO 8601 format). | ||
| - `tags` (Map of List of String) Tags of the payment method. |
This file contains hidden or 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,93 @@ | ||
| --- | ||
| # generated by https://github.com/hashicorp/terraform-plugin-docs | ||
| page_title: "meshstack_payment_method Resource - terraform-provider-meshstack" | ||
| subcategory: "" | ||
| description: |- | ||
| Represents a meshStack payment method. | ||
| ~> Note: Managing payment methods requires an API key with sufficient admin permissions. | ||
| --- | ||
|
|
||
| # meshstack_payment_method (Resource) | ||
|
|
||
| Represents a meshStack payment method. | ||
|
|
||
| ~> **Note:** Managing payment methods requires an API key with sufficient admin permissions. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ```terraform | ||
| data "meshstack_workspace" "example" { | ||
| metadata = { | ||
| name = "my-workspace-identifier" | ||
| } | ||
| } | ||
|
|
||
| resource "meshstack_payment_method" "example" { | ||
| metadata = { | ||
| name = "my-payment-method" | ||
| owned_by_workspace = data.meshstack_workspace.example.metadata.name | ||
| } | ||
|
|
||
| spec = { | ||
| display_name = "My Payment Method" | ||
| expiration_date = "2025-12-31" | ||
| amount = 10000 | ||
| tags = { | ||
| CostCenter = ["0000"] | ||
| Type = ["production"] | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| <!-- schema generated by tfplugindocs --> | ||
| ## Schema | ||
|
|
||
| ### Required | ||
|
|
||
| - `metadata` (Attributes) (see [below for nested schema](#nestedatt--metadata)) | ||
| - `spec` (Attributes) (see [below for nested schema](#nestedatt--spec)) | ||
|
|
||
| ### Read-Only | ||
|
|
||
| - `api_version` (String) Payment method datatype version | ||
| - `kind` (String) meshObject type, always `meshPaymentMethod`. | ||
|
|
||
| <a id="nestedatt--metadata"></a> | ||
| ### Nested Schema for `metadata` | ||
|
|
||
| Required: | ||
|
|
||
| - `name` (String) Payment method identifier. | ||
| - `owned_by_workspace` (String) Identifier of the workspace that owns this payment method. | ||
|
|
||
| Read-Only: | ||
|
|
||
| - `created_on` (String) Creation date of the payment method. | ||
| - `deleted_on` (String) Deletion date of the payment method. | ||
|
|
||
|
|
||
| <a id="nestedatt--spec"></a> | ||
| ### Nested Schema for `spec` | ||
|
|
||
| Required: | ||
|
|
||
| - `display_name` (String) Display name of the payment method. | ||
|
|
||
| Optional: | ||
|
|
||
| - `amount` (Number) Amount associated with the payment method. | ||
| - `expiration_date` (String) Expiration date of the payment method (ISO 8601 format). | ||
| - `tags` (Map of List of String) Tags of the payment method. | ||
|
|
||
| ## Import | ||
|
|
||
| Import is supported using the following syntax: | ||
|
|
||
| The [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import) can be used, for example: | ||
|
|
||
| ```shell | ||
| #!/bin/bash | ||
| # import via workspace and payment method identifier <workspace-identifier>.<payment-method-identifier> | ||
| terraform import 'meshstack_payment_method.example' 'my-workspace-identifier.my-payment-method' | ||
| ``` |
6 changes: 6 additions & 0 deletions
6
examples/data-sources/meshstack_payment_method/data-source.tf
This file contains hidden or 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,6 @@ | ||
| data "meshstack_payment_method" "example" { | ||
| metadata = { | ||
| name = "my-payment-method" | ||
| owned_by_workspace = "my-workspace-identifier" | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| #!/bin/bash | ||
| # import via workspace and payment method identifier <workspace-identifier>.<payment-method-identifier> | ||
| terraform import 'meshstack_payment_method.example' 'my-workspace-identifier.my-payment-method' |
This file contains hidden or 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,22 @@ | ||
| data "meshstack_workspace" "example" { | ||
| metadata = { | ||
| name = "my-workspace-identifier" | ||
| } | ||
| } | ||
|
|
||
| resource "meshstack_payment_method" "example" { | ||
| metadata = { | ||
| name = "my-payment-method" | ||
| owned_by_workspace = data.meshstack_workspace.example.metadata.name | ||
| } | ||
|
|
||
| spec = { | ||
| display_name = "My Payment Method" | ||
| expiration_date = "2025-12-31" | ||
| amount = 10000 | ||
| tags = { | ||
| CostCenter = ["0000"] | ||
| Type = ["production"] | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
d: one issue I've noticed is the following:
when you use the terraform provider to first create a payment method with an expiration date, and then remove it, you run into an error (see screenshot). But the problem imho is not the terraform provider, it's the API design. The expiration date is nullable in our meshPaymentMethod model, and if the expiration date is null in an update request, we just ignore it completely (instead of setting the expiration date to null).
imho this has to be fixed by introducing a new meshPaymentMethod version with proper null handling, meaning, nullable properties like tags, amount and expirationDate are explicitly set to null if they're null in the request.