-
Notifications
You must be signed in to change notification settings - Fork 7
/
resource_account_schema.go
176 lines (146 loc) · 4.64 KB
/
resource_account_schema.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
package main
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
)
func resourceAccountSchema() *schema.Resource {
return &schema.Resource{
Create: resourceAccountSchemaCreate,
Read: resourceAccountSchemaRead,
Update: resourceAccountSchemaUpdate,
Delete: resourceAccountSchemaDelete,
Importer: &schema.ResourceImporter{
State: resourceAccountSchemaImport,
},
Schema: accountSchemaFields(),
}
}
func resourceAccountSchemaCreate(d *schema.ResourceData, m interface{}) error {
accountSchema, err := expandAccountSchema(d)
if err != nil {
return err
}
client, err := m.(*Config).IdentityNowClient()
if err != nil {
return err
}
newAccountSchema, err := client.GetAccountSchema(context.Background(), accountSchema.SourceID, accountSchema.ID)
if err != nil {
// Handle NotFoundError and other errors as before
_, notFound := err.(*NotFoundError)
if notFound {
log.Printf("Source ID %s not found.", accountSchema.SourceID)
d.SetId("")
return nil
}
return err
}
newAccountSchema.SourceID = accountSchema.SourceID
// Use a map to track seen attributes based on a combination of fields
seen := make(map[string]bool)
result := []*AccountSchemaAttribute{}
// Iterate over accountSchema.Attributes to filter out duplicates
for _, attribute := range accountSchema.Attributes {
// Create a unique key for each attribute based on important fields (e.g., "name")
key := attribute.Name // or use a combination of "Name" and other unique fields if necessary
if _, ok := seen[key]; !ok {
seen[key] = true
result = append(result, attribute)
}
}
// Update the newAccountSchema with the filtered attributes
newAccountSchema.Attributes = result
newAccountSchema.ID = accountSchema.ID
log.Printf("[INFO] Creating Account Schema Attribute for source %+v\n", newAccountSchema.SourceID)
accountSchemaResponse, err := client.UpdateAccountSchema(context.Background(), newAccountSchema)
if err != nil {
return err
}
accountSchemaResponse.SourceID = accountSchema.SourceID
err = flattenAccountSchema(d, accountSchemaResponse)
if err != nil {
return err
}
return resourceAccountSchemaRead(d, m)
}
func resourceAccountSchemaRead(d *schema.ResourceData, m interface{}) error {
sourceId := d.Get("source_id").(string)
schemaId := d.Get("schema_id").(string)
attrName := d.Get("name").(string)
log.Printf("[INFO] Refreshing Account Schema for Source %s", sourceId)
client, err := m.(*Config).IdentityNowClient()
if err != nil {
return err
}
accountSchema, err := client.GetAccountSchema(context.Background(), sourceId, schemaId)
if err != nil {
// non-panicking type assertion, 2nd arg is boolean indicating type match
_, notFound := err.(*NotFoundError)
if notFound {
log.Printf("Source ID %s not found.", sourceId)
d.SetId("")
return nil
}
return err
}
if accountSchema.Attributes == nil {
log.Printf("Attribute %s not found in Account Schema.", attrName)
d.SetId("")
}
accountSchema.SourceID = sourceId
err = flattenAccountSchema(d, accountSchema)
if err != nil {
return err
}
return nil
}
func resourceAccountSchemaUpdate(d *schema.ResourceData, m interface{}) error {
updatedAccountSchema, err := expandAccountSchema(d)
if err != nil {
return err
}
log.Printf("[INFO] Updating %s for Account Schema for source ID %s", d.Get("name").(string), d.Get("source_id").(string))
client, err := m.(*Config).IdentityNowClient()
if err != nil {
return err
}
_, err = client.UpdateAccountSchema(context.Background(), updatedAccountSchema)
if err != nil {
return err
}
return resourceAccountSchemaRead(d, m)
}
func resourceAccountSchemaDelete(d *schema.ResourceData, m interface{}) error {
sourceId := d.Get("source_id").(string)
schemaId := d.Get("schema_id").(string)
name := d.Get("name").(string)
log.Printf("[INFO] Deleting %s from Account Schema for source ID %s", name, sourceId)
client, err := m.(*Config).IdentityNowClient()
if err != nil {
return err
}
accountSchema, err := client.GetAccountSchema(context.Background(), sourceId, schemaId)
if err != nil {
// non-panicking type assertion, 2nd arg is boolean indicating type match
_, notFound := err.(*NotFoundError)
if notFound {
log.Printf("Source ID %s not found.", sourceId)
d.SetId("")
return nil
}
return err
}
if accountSchema.Attributes == nil {
log.Printf("Attribute %s not found in Account Schema.", name)
d.SetId("")
}
accountSchema.SourceID = sourceId
err = client.DeleteAccountSchema(context.Background(), accountSchema)
if err != nil {
return fmt.Errorf("error removing Account Schema from source %s. Error: %s", sourceId, err)
}
d.SetId("")
return nil
}