-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_viettelidc_compute_flavor_access_v2.go
156 lines (127 loc) · 4.3 KB
/
resource_viettelidc_compute_flavor_access_v2.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
144
145
146
147
148
149
150
151
152
153
154
155
156
package viettelidc
import (
"context"
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
"github.com/gophercloud/gophercloud/pagination"
)
func resourceComputeFlavorAccessV2() *schema.Resource {
return &schema.Resource{
CreateContext: resourceComputeFlavorAccessV2Create,
ReadContext: resourceComputeFlavorAccessV2Read,
DeleteContext: resourceComputeFlavorAccessV2Delete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"flavor_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"tenant_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceComputeFlavorAccessV2Create(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
computeClient, err := config.ComputeV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack compute client: %s", err)
}
flavorID := d.Get("flavor_id").(string)
tenantID := d.Get("tenant_id").(string)
accessOpts := flavors.AddAccessOpts{
Tenant: tenantID,
}
log.Printf("[DEBUG] Flavor Access Options: %#v", accessOpts)
if _, err := flavors.AddAccess(computeClient, flavorID, accessOpts).Extract(); err != nil {
return diag.Errorf("Error adding access to tenant %s for flavor %s: %s", tenantID, flavorID, err)
}
id := fmt.Sprintf("%s/%s", flavorID, tenantID)
d.SetId(id)
return resourceComputeFlavorAccessV2Read(ctx, d, meta)
}
func resourceComputeFlavorAccessV2Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
computeClient, err := config.ComputeV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack compute client: %s", err)
}
flavorAccess, err := getFlavorAccess(computeClient, d)
if err != nil {
return diag.FromErr(CheckDeleted(d, err, "Error getting flavor access"))
}
d.Set("region", GetRegion(d, config))
d.Set("flavor_id", flavorAccess.FlavorID)
d.Set("tenant_id", flavorAccess.TenantID)
return nil
}
func resourceComputeFlavorAccessV2Delete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
computeClient, err := config.ComputeV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack compute client: %s", err)
}
flavorAccess, err := getFlavorAccess(computeClient, d)
if err != nil {
return diag.FromErr(CheckDeleted(d, err, "Error getting flavor access"))
}
removeAccessOpts := flavors.RemoveAccessOpts{Tenant: flavorAccess.TenantID}
log.Printf("[DEBUG] RemoveAccess Options: %#v", removeAccessOpts)
if _, err := flavors.RemoveAccess(computeClient, flavorAccess.FlavorID, removeAccessOpts).Extract(); err != nil {
return diag.FromErr(CheckDeleted(d, err, fmt.Sprintf("Error removing tenant %s access from flavor %s", flavorAccess.TenantID, flavorAccess.FlavorID)))
}
return nil
}
func parseComputeFlavorAccessID(id string) (string, string, error) {
idParts := strings.Split(id, "/")
if len(idParts) < 2 {
return "", "", fmt.Errorf("Unable to determine flavor access ID")
}
flavorID := idParts[0]
tenantID := idParts[1]
return flavorID, tenantID, nil
}
func getFlavorAccess(computeClient *gophercloud.ServiceClient, d *schema.ResourceData) (flavors.FlavorAccess, error) {
var access flavors.FlavorAccess
flavorID, tenantID, err := parseComputeFlavorAccessID(d.Id())
if err != nil {
return access, err
}
found := false
pager := flavors.ListAccesses(computeClient, flavorID)
err = pager.EachPage(func(page pagination.Page) (bool, error) {
accessList, err := flavors.ExtractAccesses(page)
if err != nil {
return false, err
}
for _, a := range accessList {
if a.TenantID == tenantID && a.FlavorID == flavorID {
access = a
found = true
return false, nil
}
}
return true, nil
})
if !found {
return access, gophercloud.ErrDefault404{}
}
return access, err
}