This repository has been archived by the owner on Jul 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
resource_organisation.go
186 lines (147 loc) · 5.42 KB
/
resource_organisation.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package form3
import (
"fmt"
form3 "github.com/form3tech-oss/terraform-provider-form3/api"
"github.com/form3tech-oss/terraform-provider-form3/client/organisations"
"github.com/form3tech-oss/terraform-provider-form3/models"
"github.com/go-openapi/runtime"
"github.com/go-openapi/strfmt"
"github.com/hashicorp/terraform/helper/schema"
"log"
)
func resourceForm3Organisation() *schema.Resource {
return &schema.Resource{
Create: resourceOrganisationCreate,
Read: resourceOrganisationRead,
Update: resourceOrganisationUpdate,
Delete: resourceOrganisationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"organisation_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"parent_organisation_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceOrganisationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
name := d.Get("name").(string)
log.Printf("[INFO] Creating organisation with name: %s", name)
organisation, err := createOrganisationFromResourceData(d)
if err != nil {
return err
}
log.Printf("[DEBUG] organisation create: %#v", organisation)
createdOrganisation, err := client.OrganisationClient.Organisations.PostUnits(organisations.NewPostUnitsParams().
WithOrganisationCreationRequest(&models.OrganisationCreation{Data: organisation}))
if err != nil {
return fmt.Errorf("failed to create organisation: %s", err)
}
d.SetId(createdOrganisation.Payload.Data.ID.String())
log.Printf("[INFO] organisation key: %s", d.Id())
return resourceOrganisationRead(d, meta)
}
func resourceOrganisationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
key := d.Id()
organisationId, _ := GetUUIDOK(d, "organisation_id")
if organisationId == "" {
organisationId = strfmt.UUID(key)
log.Printf("[INFO] Importing organisation id: %s ", key)
} else {
log.Printf("[INFO] Reading organisation for id: %s name: %s", key, d.Get("name").(string))
}
organisation, err := client.OrganisationClient.Organisations.GetUnitsID(
organisations.NewGetUnitsIDParams().WithID(organisationId))
if err != nil {
apiError, ok := err.(*runtime.APIError)
if ok && apiError.Code == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("couldn't find organisation: %s", err)
}
d.Set("organisation_id", organisation.Payload.Data.ID.String())
d.Set("parent_organisation_id", organisation.Payload.Data.OrganisationID.String())
d.Set("name", organisation.Payload.Data.Attributes.Name)
return nil
}
func resourceOrganisationUpdate(d *schema.ResourceData, meta interface{}) error {
d.Partial(false)
if d.HasChange("name") {
client := meta.(*form3.AuthenticatedClient)
organisationFromResource, err := createOrganisationFromResourceDataWithVersion(d, client)
if err != nil {
return fmt.Errorf("error updating organisation: %s", err)
}
_, err = client.OrganisationClient.Organisations.PatchUnitsID(organisations.NewPatchUnitsIDParams().
WithID(organisationFromResource.ID).
WithOrganisationUpdateRequest(&models.OrganisationUpdate{Data: organisationFromResource}))
if err != nil {
return fmt.Errorf("error updating organisation: %s", err)
}
}
return nil
}
func resourceOrganisationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
organisationFromResource, err := createOrganisationFromResourceDataWithVersion(d, client)
if err != nil {
return fmt.Errorf("error deleting organisation: %s", err)
}
log.Printf("[INFO] Deleting organisation for id: %s organisationname: %s",
organisationFromResource.ID, organisationFromResource.Attributes.Name)
_, err = client.OrganisationClient.Organisations.DeleteUnitsID(organisations.NewDeleteUnitsIDParams().
WithID(organisationFromResource.ID).
WithVersion(*organisationFromResource.Version))
if err != nil {
return fmt.Errorf("error deleting organisation: %s", err)
}
return nil
}
func createOrganisationFromResourceDataWithVersion(d *schema.ResourceData, client *form3.AuthenticatedClient) (*models.Organisation, error) {
organisation, err := createOrganisationFromResourceData(d)
version, err := getOrganisationVersion(client, organisation.ID)
if err != nil {
return nil, err
}
organisation.Version = &version
return organisation, nil
}
func createOrganisationFromResourceData(d *schema.ResourceData) (*models.Organisation, error) {
organisation := models.Organisation{Attributes: &models.OrganisationAttributes{}}
organisation.Type = "organisations"
if attr, ok := GetUUIDOK(d, "organisation_id"); ok {
organisation.ID = attr
}
if attr, ok := GetUUIDOK(d, "parent_organisation_id"); ok {
organisation.OrganisationID = attr
}
if attr, ok := d.GetOk("name"); ok {
organisation.Attributes.Name = attr.(string)
}
return &organisation, nil
}
func getOrganisationVersion(client *form3.AuthenticatedClient, organisationId strfmt.UUID) (int64, error) {
organisation, err := client.OrganisationClient.Organisations.GetUnitsID(organisations.NewGetUnitsIDParams().
WithID(organisationId))
if err != nil {
if err != nil {
return -1, fmt.Errorf("error reading organisation: %s", err)
}
}
return *organisation.Payload.Data.Version, nil
}