Skip to content

Commit

Permalink
feat: tenant resource
Browse files Browse the repository at this point in the history
  • Loading branch information
henryde committed Jun 7, 2024
1 parent 6a16bee commit 2da1237
Show file tree
Hide file tree
Showing 6 changed files with 403 additions and 1 deletion.
58 changes: 58 additions & 0 deletions docs/resources/tenant.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "meshstack_tenant Resource - terraform-provider-meshstack"
subcategory: ""
description: |-
Single tenant by workspace, project, and platform.
---

# meshstack_tenant (Resource)

Single tenant by workspace, project, and platform.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `metadata` (Attributes) Tenant metadata. Workspace, project and platform of the target tenant must be set here. (see [below for nested schema](#nestedatt--metadata))
- `spec` (Attributes) Tenant specification. (see [below for nested schema](#nestedatt--spec))

### Read-Only

- `api_version` (String) Tenant datatype version
- `kind` (String) meshObject type, always `meshTenant`.

<a id="nestedatt--metadata"></a>
### Nested Schema for `metadata`

Required:

- `owned_by_project` (String)
- `owned_by_workspace` (String)
- `platform_identifier` (String)

Read-Only:

- `assigned_tags` (Map of List of String)
- `deleted_on` (String)


<a id="nestedatt--spec"></a>
### Nested Schema for `spec`

Optional:

- `landing_zone_identifier` (String)
- `local_id` (String)
- `quotas` (Attributes List) (see [below for nested schema](#nestedatt--spec--quotas))

<a id="nestedatt--spec--quotas"></a>
### Nested Schema for `spec.quotas`

Read-Only:

- `key` (String)
- `value` (String)
66 changes: 66 additions & 0 deletions internal/provider/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,69 @@ func (c *MeshStackProviderClient) ReadTenant(workspace string, project string, p

return &tenant, nil
}

func (c *MeshStackProviderClient) CreateTenant(tenant *MeshTenantCreate) (*MeshTenant, error) {
payload, err := json.Marshal(tenant)
if err != nil {
return nil, err
}

req, err := http.NewRequest("POST", c.endpoints.Tenants.String(), bytes.NewBuffer(payload))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", CONTENT_TYPE_TENANT)
req.Header.Set("Accept", CONTENT_TYPE_TENANT)

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 res.StatusCode != 201 {
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}

var createdTenant MeshTenant
err = json.Unmarshal(data, &tenant)
if err != nil {
return nil, err
}

return &createdTenant, nil
}

func (c *MeshStackProviderClient) DeleteTenant(workspace string, project string, platform string) error {
targetUrl := c.urlForTenant(workspace, project, platform)

req, err := http.NewRequest("DELETE", targetUrl.String(), nil)
if err != nil {
return err
}

res, err := c.doAuthenticatedRequest(req)

if err != nil {
return errors.New(ERROR_GENERIC_CLIENT_ERROR)
}

defer res.Body.Close()

data, err := io.ReadAll(res.Body)
if err != nil {
return err
}

if res.StatusCode != 202 {
return fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}

return nil
}
6 changes: 5 additions & 1 deletion internal/provider/project_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ func (r *projectResource) Schema(_ context.Context, _ resource.SchemaRequest, re
MarkdownDescription: "Project specification.",
Required: true,
Attributes: map[string]schema.Attribute{

"display_name": schema.StringAttribute{Required: true},
// TODO: Blocks would be more terraform-y.
"tags": schema.MapAttribute{
Expand All @@ -127,6 +126,7 @@ func (r *projectResource) Schema(_ context.Context, _ resource.SchemaRequest, re
}
}

// These structs use Terraform types so that we can read the plan and check for unknown/null values.
type projectCreate struct {
ApiVersion types.String `json:"apiVersion" tfsdk:"api_version"`
Kind types.String `json:"kind" tfsdk:"kind"`
Expand Down Expand Up @@ -211,6 +211,10 @@ func (r *projectResource) Read(ctx context.Context, req resource.ReadRequest, re
resp.Diagnostics.Append(req.State.GetAttribute(ctx, path.Root("metadata").AtName("owned_by_workspace"), &workspace)...)
resp.Diagnostics.Append(req.State.GetAttribute(ctx, path.Root("metadata").AtName("name"), &name)...)

if resp.Diagnostics.HasError() {
return
}

project, err := r.client.ReadProject(workspace, name)
if err != nil {
resp.Diagnostics.AddError("Unable to read project", err.Error())
Expand Down
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (p *MeshStackProvider) Configure(ctx context.Context, req provider.Configur
func (p *MeshStackProvider) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewProjectResource,
NewTenantResource,
}
}

Expand Down
17 changes: 17 additions & 0 deletions internal/provider/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,20 @@ type MeshTenantQuota struct {
Key string `json:"key" tfsdk:"key"`
Value int64 `json:"value" tfsdk:"value"`
}

type MeshTenantCreate struct {
Metadata MeshTenantCreateMetadata `json:"metadata" tfsdk:"metadata"`
Spec MeshTenantCreateSpec `json:"spec" tfsdk:"spec"`
}

type MeshTenantCreateMetadata struct {
OwnedByProject string `json:"ownedByProject" tfsdk:"owned_by_project"`
OwnedByWorkspace string `json:"ownedByWorkspace" tfsdk:"owned_by_workspace"`
PlatformIdentifier string `json:"platformIdentifier" tfsdk:"platform_identifier"`
}

type MeshTenantCreateSpec struct {
LocalId *string `json:"localId" tfsdk:"local_id"`
LandingZoneIdentifier *string `json:"landingZoneIdentifier" tfsdk:"landing_zone_identifier"`
Quotas *[]MeshTenantQuota `json:"quotas" tfsdk:"quotas"`
}
Loading

0 comments on commit 2da1237

Please sign in to comment.