|
| 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 | +} |
0 commit comments