Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type endpoints struct {
TagDefinitions *url.URL `json:"meshtagdefinitions"`
LandingZones *url.URL `json:"meshlandingzones"`
Platforms *url.URL `json:"meshplatforms"`
PaymentMethods *url.URL `json:"meshpaymentmethods"`
}

type loginRequest struct {
Expand Down Expand Up @@ -80,6 +81,7 @@ func NewClient(rootUrl *url.URL, apiKey string, apiSecret string) (*MeshStackPro
TagDefinitions: rootUrl.JoinPath(apiMeshObjectsRoot, "meshtagdefinitions"),
LandingZones: rootUrl.JoinPath(apiMeshObjectsRoot, "meshlandingzones"),
Platforms: rootUrl.JoinPath(apiMeshObjectsRoot, "meshplatforms"),
PaymentMethods: rootUrl.JoinPath(apiMeshObjectsRoot, "meshpaymentmethods"),
}

return client, nil
Expand Down
169 changes: 169 additions & 0 deletions client/payment_method.go
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"`
Copy link
Contributor

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.

Image Image Image

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)
}
59 changes: 59 additions & 0 deletions docs/data-sources/payment_method.md
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.
93 changes: 93 additions & 0 deletions docs/resources/payment_method.md
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 examples/data-sources/meshstack_payment_method/data-source.tf
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"
}
}
3 changes: 3 additions & 0 deletions examples/resources/meshstack_payment_method/import.sh
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'
22 changes: 22 additions & 0 deletions examples/resources/meshstack_payment_method/resource.tf
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"]
}
}
}
Loading