|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + CONTENT_TYPE_BUILDING_BLOCK_V2 = "application/vnd.meshcloud.api.meshbuildingblock.v2-preview.hal+json" |
| 13 | +) |
| 14 | + |
| 15 | +type MeshBuildingBlockV2 struct { |
| 16 | + ApiVersion string `json:"apiVersion" tfsdk:"api_version"` |
| 17 | + Kind string `json:"kind" tfsdk:"kind"` |
| 18 | + Metadata MeshBuildingBlockV2Metadata `json:"metadata" tfsdk:"metadata"` |
| 19 | + Spec MeshBuildingBlockV2Spec `json:"spec" tfsdk:"spec"` |
| 20 | + Status MeshBuildingBlockV2Status `json:"status" tfsdk:"status"` |
| 21 | +} |
| 22 | + |
| 23 | +type MeshBuildingBlockV2Metadata struct { |
| 24 | + Uuid string `json:"uuid" tfsdk:"uuid"` |
| 25 | + OwnedByWorkspace string `json:"ownedByWorkspace" tfsdk:"owned_by_workspace"` |
| 26 | + CreatedOn string `json:"createdOn" tfsdk:"created_on"` |
| 27 | + MarkedForDeletionOn *string `json:"markedForDeletionOn" tfsdk:"marked_for_deletion_on"` |
| 28 | + MarkedForDeletionBy *string `json:"markedForDeletionBy" tfsdk:"marked_for_deletion_by"` |
| 29 | +} |
| 30 | + |
| 31 | +type MeshBuildingBlockV2Spec struct { |
| 32 | + BuildingBlockDefinitionVersionRef MeshBuildingBlockV2DefinitionVersionRef `json:"buildingBlockDefinitionVersionRef" tfsdk:"building_block_definition_version_ref"` |
| 33 | + TargetRef MeshBuildingBlockV2TargetRef `json:"targetRef" tfsdk:"target_ref"` |
| 34 | + DisplayName string `json:"displayName" tfsdk:"display_name"` |
| 35 | + |
| 36 | + Inputs []MeshBuildingBlockIO `json:"inputs" tfsdk:"inputs"` |
| 37 | + ParentBuildingBlocks []MeshBuildingBlockParent `json:"parentBuildingBlocks" tfsdk:"parent_building_blocks"` |
| 38 | +} |
| 39 | + |
| 40 | +type MeshBuildingBlockV2DefinitionVersionRef struct { |
| 41 | + Uuid string `json:"uuid" tfsdk:"uuid"` |
| 42 | +} |
| 43 | + |
| 44 | +type MeshBuildingBlockV2TargetRef struct { |
| 45 | + Kind string `json:"kind" tfsdk:"kind"` |
| 46 | + Uuid *string `json:"uuid" tfsdk:"uuid"` |
| 47 | + Identifier *string `json:"identifier" tfsdk:"identifier"` |
| 48 | +} |
| 49 | + |
| 50 | +type MeshBuildingBlockV2Create struct { |
| 51 | + ApiVersion string `json:"apiVersion" tfsdk:"api_version"` |
| 52 | + Kind string `json:"kind" tfsdk:"kind"` |
| 53 | + Spec MeshBuildingBlockV2Spec `json:"spec" tfsdk:"spec"` |
| 54 | +} |
| 55 | + |
| 56 | +type MeshBuildingBlockV2Status struct { |
| 57 | + Status string `json:"status" tfsdk:"status"` |
| 58 | + Outputs []MeshBuildingBlockIO `json:"outputs" tfsdk:"outputs"` |
| 59 | + ForcePurge bool `json:"forcePurge" tfsdk:"force_purge"` |
| 60 | +} |
| 61 | + |
| 62 | +func (c *MeshStackProviderClient) ReadBuildingBlockV2(uuid string) (*MeshBuildingBlockV2, error) { |
| 63 | + targetUrl := c.urlForBuildingBlock(uuid) |
| 64 | + |
| 65 | + req, err := http.NewRequest("GET", targetUrl.String(), nil) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + req.Header.Set("Accept", CONTENT_TYPE_BUILDING_BLOCK_V2) |
| 70 | + |
| 71 | + res, err := c.doAuthenticatedRequest(req) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + defer res.Body.Close() |
| 77 | + |
| 78 | + data, err := io.ReadAll(res.Body) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + |
| 83 | + if res.StatusCode == 404 { |
| 84 | + return nil, nil |
| 85 | + } |
| 86 | + |
| 87 | + if !isSuccessHTTPStatus(res) { |
| 88 | + return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data) |
| 89 | + } |
| 90 | + |
| 91 | + var bb MeshBuildingBlockV2 |
| 92 | + err = json.Unmarshal(data, &bb) |
| 93 | + if err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + |
| 97 | + return &bb, nil |
| 98 | +} |
| 99 | + |
| 100 | +func (c *MeshStackProviderClient) CreateBuildingBlockV2(bb *MeshBuildingBlockV2Create) (*MeshBuildingBlockV2, error) { |
| 101 | + payload, err := json.Marshal(bb) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + req, err := http.NewRequest("POST", c.endpoints.BuildingBlocks.String(), bytes.NewBuffer(payload)) |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + req.Header.Set("Content-Type", CONTENT_TYPE_BUILDING_BLOCK_V2) |
| 111 | + req.Header.Set("Accept", CONTENT_TYPE_BUILDING_BLOCK_V2) |
| 112 | + |
| 113 | + res, err := c.doAuthenticatedRequest(req) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + |
| 118 | + defer res.Body.Close() |
| 119 | + |
| 120 | + data, err := io.ReadAll(res.Body) |
| 121 | + if err != nil { |
| 122 | + return nil, err |
| 123 | + } |
| 124 | + |
| 125 | + if !isSuccessHTTPStatus(res) { |
| 126 | + return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data) |
| 127 | + } |
| 128 | + |
| 129 | + var createdBb MeshBuildingBlockV2 |
| 130 | + err = json.Unmarshal(data, &createdBb) |
| 131 | + if err != nil { |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + |
| 135 | + return &createdBb, nil |
| 136 | +} |
| 137 | + |
| 138 | +func (c *MeshStackProviderClient) DeleteBuildingBlockV2(uuid string) error { |
| 139 | + targetUrl := c.urlForBuildingBlock(uuid) |
| 140 | + return c.deleteMeshObject(*targetUrl, 202) |
| 141 | +} |
0 commit comments