|
| 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_TENANT_V4 = "application/vnd.meshcloud.api.meshtenant.v4.hal+json" |
| 13 | + |
| 14 | +type MeshTenantV4 struct { |
| 15 | + ApiVersion string `json:"apiVersion" tfsdk:"api_version"` |
| 16 | + Kind string `json:"kind" tfsdk:"kind"` |
| 17 | + Metadata MeshTenantMetadataV4 `json:"metadata" tfsdk:"metadata"` |
| 18 | + Spec MeshTenantSpecV4 `json:"spec" tfsdk:"spec"` |
| 19 | + Status MeshTenantStatusV4 `json:"status" tfsdk:"status"` |
| 20 | +} |
| 21 | + |
| 22 | +type MeshTenantMetadataV4 struct { |
| 23 | + UUID string `json:"uuid" tfsdk:"uuid"` |
| 24 | + OwnedByProject string `json:"ownedByProject" tfsdk:"owned_by_project"` |
| 25 | + OwnedByWorkspace string `json:"ownedByWorkspace" tfsdk:"owned_by_workspace"` |
| 26 | + DeletedOn *string `json:"deletedOn" tfsdk:"deleted_on"` |
| 27 | + CreatedOn *string `json:"createdOn" tfsdk:"created_on"` |
| 28 | +} |
| 29 | + |
| 30 | +type MeshTenantSpecV4 struct { |
| 31 | + PlatformIdentifier string `json:"platformIdentifier" tfsdk:"platform_identifier"` |
| 32 | + LocalId *string `json:"localId" tfsdk:"local_id"` |
| 33 | + LandingZoneIdentifier string `json:"landingZoneIdentifier" tfsdk:"landing_zone_identifier"` |
| 34 | + Quotas []MeshTenantQuota `json:"quotas" tfsdk:"quotas"` |
| 35 | +} |
| 36 | + |
| 37 | +type MeshTenantStatusV4 struct { |
| 38 | + Tags map[string][]string `json:"tags" tfsdk:"tags"` |
| 39 | + LastReplicated *string `json:"lastReplicated" tfsdk:"last_replicated"` |
| 40 | + CurrentReplicationStatus string `json:"currentReplicationStatus" tfsdk:"current_replication_status"` |
| 41 | +} |
| 42 | + |
| 43 | +type MeshTenantCreateV4 struct { |
| 44 | + Metadata MeshTenantCreateMetadataV4 `json:"metadata" tfsdk:"metadata"` |
| 45 | + Spec MeshTenantCreateSpecV4 `json:"spec" tfsdk:"spec"` |
| 46 | +} |
| 47 | + |
| 48 | +type MeshTenantCreateMetadataV4 struct { |
| 49 | + UUID string `json:"uuid" tfsdk:"uuid"` |
| 50 | + OwnedByProject string `json:"ownedByProject" tfsdk:"owned_by_project"` |
| 51 | + OwnedByWorkspace string `json:"ownedByWorkspace" tfsdk:"owned_by_workspace"` |
| 52 | +} |
| 53 | + |
| 54 | +type MeshTenantCreateSpecV4 struct { |
| 55 | + PlatformIdentifier string `json:"platformIdentifier" tfsdk:"platform_identifier"` |
| 56 | + LocalId *string `json:"localId" tfsdk:"local_id"` |
| 57 | + LandingZoneIdentifier string `json:"landingZoneIdentifier" tfsdk:"landing_zone_identifier"` |
| 58 | + Quotas []MeshTenantQuota `json:"quotas" tfsdk:"quotas"` |
| 59 | +} |
| 60 | + |
| 61 | +func (c *MeshStackProviderClient) urlForTenantV4(uuid string) *url.URL { |
| 62 | + return c.endpoints.Tenants.JoinPath(uuid) |
| 63 | +} |
| 64 | + |
| 65 | +func (c *MeshStackProviderClient) ReadTenantV4(uuid string) (*MeshTenantV4, error) { |
| 66 | + targetUrl := c.urlForTenantV4(uuid) |
| 67 | + req, err := http.NewRequest("GET", targetUrl.String(), nil) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + req.Header.Set("Accept", CONTENT_TYPE_TENANT_V4) |
| 72 | + |
| 73 | + res, err := c.doAuthenticatedRequest(req) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + |
| 78 | + defer res.Body.Close() |
| 79 | + |
| 80 | + data, err := io.ReadAll(res.Body) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + if res.StatusCode == 404 { |
| 86 | + return nil, nil |
| 87 | + } |
| 88 | + |
| 89 | + if res.StatusCode != 200 { |
| 90 | + return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data) |
| 91 | + } |
| 92 | + |
| 93 | + var tenant MeshTenantV4 |
| 94 | + err = json.Unmarshal(data, &tenant) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + return &tenant, nil |
| 100 | +} |
| 101 | + |
| 102 | +func (c *MeshStackProviderClient) CreateTenantV4(tenant *MeshTenantCreateV4) (*MeshTenantV4, error) { |
| 103 | + payload, err := json.Marshal(tenant) |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + |
| 108 | + req, err := http.NewRequest("POST", c.endpoints.Tenants.String(), bytes.NewBuffer(payload)) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + req.Header.Set("Content-Type", CONTENT_TYPE_TENANT_V4) |
| 113 | + req.Header.Set("Accept", CONTENT_TYPE_TENANT_V4) |
| 114 | + |
| 115 | + res, err := c.doAuthenticatedRequest(req) |
| 116 | + if err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + |
| 120 | + defer res.Body.Close() |
| 121 | + |
| 122 | + data, err := io.ReadAll(res.Body) |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + |
| 127 | + if res.StatusCode != 200 { |
| 128 | + return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data) |
| 129 | + } |
| 130 | + |
| 131 | + var createdTenant MeshTenantV4 |
| 132 | + err = json.Unmarshal(data, &createdTenant) |
| 133 | + if err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + |
| 137 | + return &createdTenant, nil |
| 138 | +} |
| 139 | + |
| 140 | +func (c *MeshStackProviderClient) DeleteTenantV4(uuid string) error { |
| 141 | + targetUrl := c.urlForTenantV4(uuid) |
| 142 | + return c.deleteMeshObject(*targetUrl, 202) |
| 143 | +} |
0 commit comments