|
| 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_LANDINGZONE = "application/vnd.meshcloud.api.meshlandingzone.v1-preview.hal+json" |
| 13 | + |
| 14 | +type MeshLandingZone struct { |
| 15 | + ApiVersion string `json:"apiVersion" tfsdk:"api_version"` |
| 16 | + Kind string `json:"kind" tfsdk:"kind"` |
| 17 | + Metadata MeshLandingZoneMetadata `json:"metadata" tfsdk:"metadata"` |
| 18 | + Spec MeshLandingZoneSpec `json:"spec" tfsdk:"spec"` |
| 19 | +} |
| 20 | + |
| 21 | +type MeshLandingZoneMetadata struct { |
| 22 | + Name string `json:"name" tfsdk:"name"` |
| 23 | + Tags map[string][]string `json:"tags" tfsdk:"tags"` |
| 24 | +} |
| 25 | + |
| 26 | +type MeshLandingZoneSpec struct { |
| 27 | + DisplayName string `json:"displayName" tfsdk:"display_name"` |
| 28 | + Description string `json:"description" tfsdk:"description"` |
| 29 | + AutomateDeletionApproval bool `json:"automateDeletionApproval" tfsdk:"automate_deletion_approval"` |
| 30 | + AutomateDeletionReplication bool `json:"automateDeletionReplication" tfsdk:"automate_deletion_replication"` |
| 31 | + InfoLink string `json:"infoLink" tfsdk:"info_link"` |
| 32 | + PlatformRef PlatformRef `json:"platformRef" tfsdk:"platform_ref"` |
| 33 | + PlatformProperties *PlatformProperties `json:"platformProperties,omitempty" tfsdk:"platform_properties"` |
| 34 | +} |
| 35 | + |
| 36 | +type PlatformRef struct { |
| 37 | + Uuid string `json:"uuid" tfsdk:"uuid"` |
| 38 | + Kind string `json:"kind" tfsdk:"kind"` |
| 39 | +} |
| 40 | + |
| 41 | +type PlatformProperties struct { |
| 42 | + Type string `json:"type" tfsdk:"type"` |
| 43 | + AWS *AwsPlatformProperties `json:"aws" tfsdk:"aws"` |
| 44 | + AKS *AksPlatformProperties `json:"aks" tfsdk:"aks"` |
| 45 | + Azure *AzurePlatformProperties `json:"azure" tfsdk:"azure"` |
| 46 | + AzureRG *AzureRgPlatformProperties `json:"azurerg" tfsdk:"azurerg"` |
| 47 | + GCP *GcpPlatformProperties `json:"gcp" tfsdk:"gcp"` |
| 48 | + Kubernetes *KubernetesPlatformProperties `json:"kubernetes" tfsdk:"kubernetes"` |
| 49 | + OpenShift *OpenShiftPlatformProperties `json:"openshift" tfsdk:"openshift"` |
| 50 | +} |
| 51 | + |
| 52 | +type MeshLandingZoneCreate struct { |
| 53 | + ApiVersion string `json:"apiVersion" tfsdk:"api_version"` |
| 54 | + Metadata MeshLandingZoneCreateMetadata `json:"metadata" tfsdk:"metadata"` |
| 55 | + Spec MeshLandingZoneSpec `json:"spec" tfsdk:"spec"` |
| 56 | +} |
| 57 | +type MeshLandingZoneCreateMetadata struct { |
| 58 | + Name string `json:"name" tfsdk:"name"` |
| 59 | + Tags map[string][]string `json:"tags" tfsdk:"tags"` |
| 60 | +} |
| 61 | + |
| 62 | +func (c *MeshStackProviderClient) urlForLandingZone(name string) *url.URL { |
| 63 | + return c.endpoints.LandingZones.JoinPath(name) |
| 64 | +} |
| 65 | + |
| 66 | +func (c *MeshStackProviderClient) ReadLandingZone(name string) (*MeshLandingZone, error) { |
| 67 | + targetUrl := c.urlForLandingZone(name) |
| 68 | + req, err := http.NewRequest("GET", targetUrl.String(), nil) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + req.Header.Set("Accept", CONTENT_TYPE_LANDINGZONE) |
| 73 | + |
| 74 | + res, err := c.doAuthenticatedRequest(req) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + defer res.Body.Close() |
| 80 | + |
| 81 | + if res.StatusCode == http.StatusNotFound { |
| 82 | + return nil, nil // Not found is not an error |
| 83 | + } |
| 84 | + |
| 85 | + data, err := io.ReadAll(res.Body) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + if !isSuccessHTTPStatus(res) { |
| 91 | + return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data) |
| 92 | + } |
| 93 | + |
| 94 | + var landingZone MeshLandingZone |
| 95 | + err = json.Unmarshal(data, &landingZone) |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + return &landingZone, nil |
| 100 | +} |
| 101 | + |
| 102 | +func (c *MeshStackProviderClient) CreateLandingZone(landingZone *MeshLandingZoneCreate) (*MeshLandingZone, error) { |
| 103 | + payload, err := json.Marshal(landingZone) |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + |
| 108 | + req, err := http.NewRequest("POST", c.endpoints.LandingZones.String(), bytes.NewBuffer(payload)) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + req.Header.Set("Content-Type", CONTENT_TYPE_LANDINGZONE) |
| 113 | + req.Header.Set("Accept", CONTENT_TYPE_LANDINGZONE) |
| 114 | + |
| 115 | + res, err := c.doAuthenticatedRequest(req) |
| 116 | + if err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + defer res.Body.Close() |
| 120 | + |
| 121 | + data, err := io.ReadAll(res.Body) |
| 122 | + if err != nil { |
| 123 | + return nil, err |
| 124 | + } |
| 125 | + |
| 126 | + if !isSuccessHTTPStatus(res) { |
| 127 | + return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data) |
| 128 | + } |
| 129 | + |
| 130 | + var createdLandingZone MeshLandingZone |
| 131 | + err = json.Unmarshal(data, &createdLandingZone) |
| 132 | + if err != nil { |
| 133 | + return nil, err |
| 134 | + } |
| 135 | + return &createdLandingZone, nil |
| 136 | +} |
| 137 | + |
| 138 | +func (c *MeshStackProviderClient) UpdateLandingZone(name string, landingZone *MeshLandingZoneCreate) (*MeshLandingZone, error) { |
| 139 | + targetUrl := c.urlForLandingZone(name) |
| 140 | + |
| 141 | + payload, err := json.Marshal(landingZone) |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + |
| 146 | + req, err := http.NewRequest("PUT", targetUrl.String(), bytes.NewBuffer(payload)) |
| 147 | + if err != nil { |
| 148 | + return nil, err |
| 149 | + } |
| 150 | + req.Header.Set("Content-Type", CONTENT_TYPE_LANDINGZONE) |
| 151 | + req.Header.Set("Accept", CONTENT_TYPE_LANDINGZONE) |
| 152 | + |
| 153 | + res, err := c.doAuthenticatedRequest(req) |
| 154 | + if err != nil { |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + defer res.Body.Close() |
| 158 | + |
| 159 | + data, err := io.ReadAll(res.Body) |
| 160 | + if err != nil { |
| 161 | + return nil, err |
| 162 | + } |
| 163 | + |
| 164 | + if !isSuccessHTTPStatus(res) { |
| 165 | + return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data) |
| 166 | + } |
| 167 | + |
| 168 | + var updatedLandingZone MeshLandingZone |
| 169 | + err = json.Unmarshal(data, &updatedLandingZone) |
| 170 | + if err != nil { |
| 171 | + return nil, err |
| 172 | + } |
| 173 | + return &updatedLandingZone, nil |
| 174 | +} |
| 175 | + |
| 176 | +func (c *MeshStackProviderClient) DeleteLandingZone(name string) error { |
| 177 | + targetUrl := c.urlForLandingZone(name) |
| 178 | + return c.deleteMeshObject(*targetUrl, 204) |
| 179 | +} |
0 commit comments