/
tenant.go
143 lines (115 loc) · 4.52 KB
/
tenant.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright 2023 Pure Storage Inc
SPDX-License-Identifier: Apache-2.0
*/
package fusion
import (
"context"
"fmt"
"github.com/PureStorage-OpenConnect/terraform-provider-fusion/internal/utilities"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
hmrest "github.com/PureStorage-OpenConnect/terraform-provider-fusion/internal/hmrest"
)
// Implements ResourceProvider
type tenantProvider struct {
BaseResourceProvider
}
func schemaTenant() map[string]*schema.Schema {
return map[string]*schema.Schema{
optionName: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
Description: "The name of the Tenant.",
},
optionDisplayName: {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringIsNotEmpty,
Description: "The human-readable name of the tenant. If not provided, defaults to I(name).",
},
}
}
func resourceTenant() *schema.Resource {
p := &tenantProvider{BaseResourceProvider{ResourceKind: resourceKindTenant}}
tenantResourceFunctions := NewBaseResourceFunctions(resourceKindTenant, p)
tenantResourceFunctions.Resource.Description = `A Tenant (e.g. "Engineering") is created within` +
` an Organization to group Tenant Spaces. It enables departments within a company to operate autonomously.` +
` It is created by an AZ Admin.`
tenantResourceFunctions.Resource.Schema = schemaTenant()
return tenantResourceFunctions.Resource
}
func (p *tenantProvider) PrepareCreate(ctx context.Context, d *schema.ResourceData) (InvokeWriteAPI, ResourcePost, error) {
name := rdString(ctx, d, optionName)
displayName := rdStringDefault(ctx, d, optionDisplayName, name)
body := hmrest.TenantPost{
Name: name,
DisplayName: displayName,
}
fn := func(ctx context.Context, client *hmrest.APIClient, body RequestSpec) (*hmrest.Operation, error) {
op, _, err := client.TenantsApi.CreateTenant(ctx, *body.(*hmrest.TenantPost), nil)
return &op, err
}
return fn, &body, nil
}
func (p *tenantProvider) ReadResource(ctx context.Context, client *hmrest.APIClient, d *schema.ResourceData) error {
t, _, err := client.TenantsApi.GetTenantById(ctx, d.Id(), nil)
if err != nil {
return err
}
return p.loadTenant(t, d)
}
func (p *tenantProvider) PrepareDelete(ctx context.Context, client *hmrest.APIClient, d *schema.ResourceData) (InvokeWriteAPI, error) {
name := rdString(ctx, d, optionName)
fn := func(ctx context.Context, client *hmrest.APIClient, body RequestSpec) (*hmrest.Operation, error) {
op, _, err := client.TenantsApi.DeleteTenant(ctx, name, nil)
return &op, err
}
return fn, nil
}
func (p *tenantProvider) PrepareUpdate(ctx context.Context, client *hmrest.APIClient, d *schema.ResourceData) (InvokeWriteAPI, []ResourcePatch, error) {
var patches []ResourcePatch
name := rdString(ctx, d, optionName)
if d.HasChangeExcept(optionDisplayName) {
d.Partial(true)
return nil, nil, fmt.Errorf("attempting to update an immutable field")
}
displayName := rdStringDefault(ctx, d, optionDisplayName, name)
tflog.Info(ctx, "Updating", optionDisplayName, displayName)
patches = append(patches, &hmrest.TenantPatch{
DisplayName: &hmrest.NullableString{Value: displayName},
})
fn := func(ctx context.Context, client *hmrest.APIClient, body RequestSpec) (*hmrest.Operation, error) {
op, _, err := client.TenantsApi.UpdateTenant(ctx, *body.(*hmrest.TenantPatch), name, nil)
return &op, err
}
return fn, patches, nil
}
func (p *tenantProvider) ImportResource(ctx context.Context, client *hmrest.APIClient, d *schema.ResourceData) ([]*schema.ResourceData, error) {
var orderedRequiredGroupNames = []string{resourceGroupNameTenant}
// The ID is user provided value - we expect self link
selfLinkFieldsWithValues, err := utilities.ParseSelfLink(d.Id(), orderedRequiredGroupNames)
if err != nil {
return nil, fmt.Errorf("invalid tenant import path. Expected path in format '/tenants/<tenant>'")
}
tenant, _, err := client.TenantsApi.GetTenant(ctx, selfLinkFieldsWithValues[resourceGroupNameTenant], nil)
if err != nil {
utilities.TraceError(ctx, err)
return nil, err
}
err = p.loadTenant(tenant, d)
if err != nil {
return nil, err
}
d.SetId(tenant.Id)
return []*schema.ResourceData{d}, nil
}
func (vp *tenantProvider) loadTenant(tenant hmrest.Tenant, d *schema.ResourceData) error {
return getFirstError(
d.Set(optionName, tenant.Name),
d.Set(optionDisplayName, tenant.DisplayName),
)
}