Skip to content

Commit 7c30e82

Browse files
authored
Merge pull request #74 from meshcloud/feature/adding-payment-method
feat: added payment method resource and data endpoint
2 parents 5ce99b5 + b43896f commit 7c30e82

File tree

10 files changed

+775
-0
lines changed

10 files changed

+775
-0
lines changed

client/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type endpoints struct {
4444
TagDefinitions *url.URL `json:"meshtagdefinitions"`
4545
LandingZones *url.URL `json:"meshlandingzones"`
4646
Platforms *url.URL `json:"meshplatforms"`
47+
PaymentMethods *url.URL `json:"meshpaymentmethods"`
4748
}
4849

4950
type loginRequest struct {
@@ -80,6 +81,7 @@ func NewClient(rootUrl *url.URL, apiKey string, apiSecret string) (*MeshStackPro
8081
TagDefinitions: rootUrl.JoinPath(apiMeshObjectsRoot, "meshtagdefinitions"),
8182
LandingZones: rootUrl.JoinPath(apiMeshObjectsRoot, "meshlandingzones"),
8283
Platforms: rootUrl.JoinPath(apiMeshObjectsRoot, "meshplatforms"),
84+
PaymentMethods: rootUrl.JoinPath(apiMeshObjectsRoot, "meshpaymentmethods"),
8385
}
8486

8587
return client, nil

client/payment_method.go

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package client
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"net/http"
9+
"net/url"
10+
)
11+
12+
const CONTENT_TYPE_PAYMENT_METHOD = "application/vnd.meshcloud.api.meshpaymentmethod.v2.hal+json"
13+
14+
type MeshPaymentMethod struct {
15+
ApiVersion string `json:"apiVersion" tfsdk:"api_version"`
16+
Kind string `json:"kind" tfsdk:"kind"`
17+
Metadata MeshPaymentMethodMetadata `json:"metadata" tfsdk:"metadata"`
18+
Spec MeshPaymentMethodSpec `json:"spec" tfsdk:"spec"`
19+
}
20+
21+
type MeshPaymentMethodMetadata struct {
22+
Name string `json:"name" tfsdk:"name"`
23+
OwnedByWorkspace string `json:"ownedByWorkspace" tfsdk:"owned_by_workspace"`
24+
CreatedOn string `json:"createdOn" tfsdk:"created_on"`
25+
DeletedOn *string `json:"deletedOn" tfsdk:"deleted_on"`
26+
}
27+
28+
type MeshPaymentMethodSpec struct {
29+
DisplayName string `json:"displayName" tfsdk:"display_name"`
30+
ExpirationDate *string `json:"expirationDate,omitempty" tfsdk:"expiration_date"`
31+
Amount *int64 `json:"amount,omitempty" tfsdk:"amount"`
32+
Tags map[string][]string `json:"tags,omitempty" tfsdk:"tags"`
33+
}
34+
35+
type MeshPaymentMethodCreate struct {
36+
ApiVersion string `json:"apiVersion" tfsdk:"api_version"`
37+
Metadata MeshPaymentMethodCreateMetadata `json:"metadata" tfsdk:"metadata"`
38+
Spec MeshPaymentMethodSpec `json:"spec" tfsdk:"spec"`
39+
}
40+
41+
type MeshPaymentMethodCreateMetadata struct {
42+
Name string `json:"name" tfsdk:"name"`
43+
OwnedByWorkspace string `json:"ownedByWorkspace" tfsdk:"owned_by_workspace"`
44+
}
45+
46+
func (c *MeshStackProviderClient) urlForPaymentMethod(identifier string) *url.URL {
47+
return c.endpoints.PaymentMethods.JoinPath(identifier)
48+
}
49+
50+
func (c *MeshStackProviderClient) ReadPaymentMethod(workspace string, identifier string) (*MeshPaymentMethod, error) {
51+
targetUrl := c.urlForPaymentMethod(identifier)
52+
53+
req, err := http.NewRequest("GET", targetUrl.String(), nil)
54+
if err != nil {
55+
return nil, err
56+
}
57+
req.Header.Set("Accept", CONTENT_TYPE_PAYMENT_METHOD)
58+
59+
res, err := c.doAuthenticatedRequest(req)
60+
if err != nil {
61+
return nil, err
62+
}
63+
64+
defer res.Body.Close()
65+
66+
if res.StatusCode == http.StatusNotFound {
67+
return nil, nil
68+
}
69+
70+
data, err := io.ReadAll(res.Body)
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
if !isSuccessHTTPStatus(res) {
76+
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
77+
}
78+
79+
var paymentMethod MeshPaymentMethod
80+
err = json.Unmarshal(data, &paymentMethod)
81+
if err != nil {
82+
return nil, err
83+
}
84+
85+
return &paymentMethod, nil
86+
}
87+
88+
func (c *MeshStackProviderClient) CreatePaymentMethod(paymentMethod *MeshPaymentMethodCreate) (*MeshPaymentMethod, error) {
89+
payload, err := json.Marshal(paymentMethod)
90+
if err != nil {
91+
return nil, err
92+
}
93+
94+
req, err := http.NewRequest("POST", c.endpoints.PaymentMethods.String(), bytes.NewBuffer(payload))
95+
if err != nil {
96+
return nil, err
97+
}
98+
req.Header.Set("Content-Type", CONTENT_TYPE_PAYMENT_METHOD)
99+
req.Header.Set("Accept", CONTENT_TYPE_PAYMENT_METHOD)
100+
101+
res, err := c.doAuthenticatedRequest(req)
102+
if err != nil {
103+
return nil, err
104+
}
105+
106+
defer res.Body.Close()
107+
108+
data, err := io.ReadAll(res.Body)
109+
if err != nil {
110+
return nil, err
111+
}
112+
113+
if !isSuccessHTTPStatus(res) {
114+
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
115+
}
116+
117+
var createdPaymentMethod MeshPaymentMethod
118+
err = json.Unmarshal(data, &createdPaymentMethod)
119+
if err != nil {
120+
return nil, err
121+
}
122+
123+
return &createdPaymentMethod, nil
124+
}
125+
126+
func (c *MeshStackProviderClient) UpdatePaymentMethod(identifier string, paymentMethod *MeshPaymentMethodCreate) (*MeshPaymentMethod, error) {
127+
targetUrl := c.urlForPaymentMethod(identifier)
128+
129+
payload, err := json.Marshal(paymentMethod)
130+
if err != nil {
131+
return nil, err
132+
}
133+
134+
req, err := http.NewRequest("PUT", targetUrl.String(), bytes.NewBuffer(payload))
135+
if err != nil {
136+
return nil, err
137+
}
138+
req.Header.Set("Content-Type", CONTENT_TYPE_PAYMENT_METHOD)
139+
req.Header.Set("Accept", CONTENT_TYPE_PAYMENT_METHOD)
140+
141+
res, err := c.doAuthenticatedRequest(req)
142+
if err != nil {
143+
return nil, err
144+
}
145+
146+
defer res.Body.Close()
147+
148+
data, err := io.ReadAll(res.Body)
149+
if err != nil {
150+
return nil, err
151+
}
152+
153+
if !isSuccessHTTPStatus(res) {
154+
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
155+
}
156+
157+
var updatedPaymentMethod MeshPaymentMethod
158+
err = json.Unmarshal(data, &updatedPaymentMethod)
159+
if err != nil {
160+
return nil, err
161+
}
162+
163+
return &updatedPaymentMethod, nil
164+
}
165+
166+
func (c *MeshStackProviderClient) DeletePaymentMethod(identifier string) error {
167+
targetUrl := c.urlForPaymentMethod(identifier)
168+
return c.deleteMeshObject(*targetUrl, 204)
169+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "meshstack_payment_method Data Source - terraform-provider-meshstack"
4+
subcategory: ""
5+
description: |-
6+
Read a single payment method by workspace and identifier.
7+
---
8+
9+
# meshstack_payment_method (Data Source)
10+
11+
Read a single payment method by workspace and identifier.
12+
13+
## Example Usage
14+
15+
```terraform
16+
data "meshstack_payment_method" "example" {
17+
metadata = {
18+
name = "my-payment-method"
19+
owned_by_workspace = "my-workspace-identifier"
20+
}
21+
}
22+
```
23+
24+
<!-- schema generated by tfplugindocs -->
25+
## Schema
26+
27+
### Required
28+
29+
- `metadata` (Attributes) (see [below for nested schema](#nestedatt--metadata))
30+
31+
### Read-Only
32+
33+
- `api_version` (String) Payment method API version.
34+
- `kind` (String) meshObject type, always `meshPaymentMethod`.
35+
- `spec` (Attributes) (see [below for nested schema](#nestedatt--spec))
36+
37+
<a id="nestedatt--metadata"></a>
38+
### Nested Schema for `metadata`
39+
40+
Required:
41+
42+
- `name` (String) Payment method identifier.
43+
- `owned_by_workspace` (String) Identifier of the workspace that owns this payment method.
44+
45+
Read-Only:
46+
47+
- `created_on` (String) Creation date of the payment method.
48+
- `deleted_on` (String) Deletion date of the payment method.
49+
50+
51+
<a id="nestedatt--spec"></a>
52+
### Nested Schema for `spec`
53+
54+
Read-Only:
55+
56+
- `amount` (Number) Amount associated with the payment method.
57+
- `display_name` (String) Display name of the payment method.
58+
- `expiration_date` (String) Expiration date of the payment method (ISO 8601 format).
59+
- `tags` (Map of List of String) Tags of the payment method.

docs/resources/payment_method.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "meshstack_payment_method Resource - terraform-provider-meshstack"
4+
subcategory: ""
5+
description: |-
6+
Represents a meshStack payment method.
7+
~> Note: Managing payment methods requires an API key with sufficient admin permissions.
8+
---
9+
10+
# meshstack_payment_method (Resource)
11+
12+
Represents a meshStack payment method.
13+
14+
~> **Note:** Managing payment methods requires an API key with sufficient admin permissions.
15+
16+
## Example Usage
17+
18+
```terraform
19+
data "meshstack_workspace" "example" {
20+
metadata = {
21+
name = "my-workspace-identifier"
22+
}
23+
}
24+
25+
resource "meshstack_payment_method" "example" {
26+
metadata = {
27+
name = "my-payment-method"
28+
owned_by_workspace = data.meshstack_workspace.example.metadata.name
29+
}
30+
31+
spec = {
32+
display_name = "My Payment Method"
33+
expiration_date = "2025-12-31"
34+
amount = 10000
35+
tags = {
36+
CostCenter = ["0000"]
37+
Type = ["production"]
38+
}
39+
}
40+
}
41+
```
42+
43+
<!-- schema generated by tfplugindocs -->
44+
## Schema
45+
46+
### Required
47+
48+
- `metadata` (Attributes) (see [below for nested schema](#nestedatt--metadata))
49+
- `spec` (Attributes) (see [below for nested schema](#nestedatt--spec))
50+
51+
### Read-Only
52+
53+
- `api_version` (String) Payment method datatype version
54+
- `kind` (String) meshObject type, always `meshPaymentMethod`.
55+
56+
<a id="nestedatt--metadata"></a>
57+
### Nested Schema for `metadata`
58+
59+
Required:
60+
61+
- `name` (String) Payment method identifier.
62+
- `owned_by_workspace` (String) Identifier of the workspace that owns this payment method.
63+
64+
Read-Only:
65+
66+
- `created_on` (String) Creation date of the payment method.
67+
- `deleted_on` (String) Deletion date of the payment method.
68+
69+
70+
<a id="nestedatt--spec"></a>
71+
### Nested Schema for `spec`
72+
73+
Required:
74+
75+
- `display_name` (String) Display name of the payment method.
76+
77+
Optional:
78+
79+
- `amount` (Number) Amount associated with the payment method.
80+
- `expiration_date` (String) Expiration date of the payment method (ISO 8601 format).
81+
- `tags` (Map of List of String) Tags of the payment method.
82+
83+
## Import
84+
85+
Import is supported using the following syntax:
86+
87+
The [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import) can be used, for example:
88+
89+
```shell
90+
#!/bin/bash
91+
# import via workspace and payment method identifier <workspace-identifier>.<payment-method-identifier>
92+
terraform import 'meshstack_payment_method.example' 'my-workspace-identifier.my-payment-method'
93+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
data "meshstack_payment_method" "example" {
2+
metadata = {
3+
name = "my-payment-method"
4+
owned_by_workspace = "my-workspace-identifier"
5+
}
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
# import via workspace and payment method identifier <workspace-identifier>.<payment-method-identifier>
3+
terraform import 'meshstack_payment_method.example' 'my-workspace-identifier.my-payment-method'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
data "meshstack_workspace" "example" {
2+
metadata = {
3+
name = "my-workspace-identifier"
4+
}
5+
}
6+
7+
resource "meshstack_payment_method" "example" {
8+
metadata = {
9+
name = "my-payment-method"
10+
owned_by_workspace = data.meshstack_workspace.example.metadata.name
11+
}
12+
13+
spec = {
14+
display_name = "My Payment Method"
15+
expiration_date = "2025-12-31"
16+
amount = 10000
17+
tags = {
18+
CostCenter = ["0000"]
19+
Type = ["production"]
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)