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_ace.go
156 lines (126 loc) · 3.94 KB
/
resource_ace.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 form3
import (
"fmt"
form3 "github.com/form3tech-oss/terraform-provider-form3/api"
"github.com/form3tech-oss/terraform-provider-form3/client/ace"
"github.com/form3tech-oss/terraform-provider-form3/models"
"github.com/go-openapi/runtime"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
)
func resourceForm3Ace() *schema.Resource {
return &schema.Resource{
Create: resourceAceCreate,
Read: resourceAceRead,
Delete: resourceAceDelete,
Schema: map[string]*schema.Schema{
"ace_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"role_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"organisation_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"action": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"record_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
aceId := d.Get("ace_id").(string)
log.Printf("[INFO] Creating ace with id: %s", aceId)
aceResource, err := createRoleAceFromResourceData(d)
if err != nil {
return err
}
log.Printf("[DEBUG] ace create: %#v", aceResource)
createdRole, err := client.SecurityClient.Ace.PostRolesRoleIDAces(ace.NewPostRolesRoleIDAcesParams().
WithRoleID(aceResource.Attributes.RoleID).
WithAceCreationRequest(&models.AceCreation{
Data: aceResource,
}))
if err != nil {
return fmt.Errorf("failed to create ace: %s", err)
}
d.SetId(createdRole.Payload.Data.ID.String())
log.Printf("[INFO] ace key: %s", d.Id())
return resourceAceRead(d, meta)
}
func resourceAceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
aceId, _ := GetUUIDOK(d, "ace_id")
roleId, _ := GetUUIDOK(d, "role_id")
log.Printf("[INFO] Reading ace for id: %s role id: %s", aceId, roleId)
aceResponse, err := client.SecurityClient.Ace.GetRolesRoleIDAcesAceID(ace.NewGetRolesRoleIDAcesAceIDParams().
WithAceID(aceId).
WithRoleID(roleId))
if err != nil {
apiError, ok := err.(*runtime.APIError)
if ok && apiError.Code == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("couldn't find ace: %s", err)
}
d.Set("ace_id", aceResponse.Payload.Data.ID.String())
d.Set("role_id", aceResponse.Payload.Data.Attributes.RoleID.String())
d.Set("record_type", aceResponse.Payload.Data.Attributes.RecordType)
d.Set("action", aceResponse.Payload.Data.Attributes.Action)
return nil
}
func resourceAceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
aceFromResource, err := createRoleAceFromResourceData(d)
if err != nil {
return fmt.Errorf("error deleting ace: %s", err)
}
log.Printf("[INFO] Deleting ace for id: %s role id: %s", aceFromResource.ID, aceFromResource.Attributes.RoleID)
_, err = client.SecurityClient.Ace.DeleteRolesRoleIDAcesAceID(ace.NewDeleteRolesRoleIDAcesAceIDParams().
WithAceID(aceFromResource.ID).
WithRoleID(aceFromResource.Attributes.RoleID))
apiError, ok := err.(*runtime.APIError)
if ok && apiError.Code == 404 {
return nil
}
if err != nil {
return fmt.Errorf("error deleting ace: %s", err)
}
return nil
}
func createRoleAceFromResourceData(d *schema.ResourceData) (*models.Ace, error) {
ace := models.Ace{Attributes: &models.AceAttributes{}}
ace.Type = "ace"
if attr, ok := GetUUIDOK(d, "ace_id"); ok {
ace.ID = attr
}
if attr, ok := GetUUIDOK(d, "organisation_id"); ok {
ace.OrganisationID = attr
}
if attr, ok := GetUUIDOK(d, "role_id"); ok {
ace.Attributes.RoleID = attr
}
if attr, ok := d.GetOk("action"); ok {
ace.Attributes.Action = attr.(string)
}
if attr, ok := d.GetOk("record_type"); ok {
ace.Attributes.RecordType = attr.(string)
}
return &ace, nil
}