-
Notifications
You must be signed in to change notification settings - Fork 21
/
data_source_group.go
189 lines (175 loc) · 4.84 KB
/
data_source_group.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
187
188
189
package ionoscloud
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
ionoscloud "github.com/ionos-cloud/sdk-go/v6"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
)
func dataSourceGroup() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGroupRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"create_datacenter": {
Type: schema.TypeBool,
Computed: true,
},
"create_snapshot": {
Type: schema.TypeBool,
Computed: true,
},
"reserve_ip": {
Type: schema.TypeBool,
Computed: true,
},
"access_activity_log": {
Type: schema.TypeBool,
Computed: true,
},
"create_pcc": {
Type: schema.TypeBool,
Computed: true,
},
"s3_privilege": {
Type: schema.TypeBool,
Computed: true,
},
"create_backup_unit": {
Type: schema.TypeBool,
Computed: true,
},
"create_internet_access": {
Type: schema.TypeBool,
Computed: true,
},
"create_k8s_cluster": {
Type: schema.TypeBool,
Computed: true,
},
"create_flow_log": {
Type: schema.TypeBool,
Description: "Create Flow Logs privilege.",
Computed: true,
},
"access_and_manage_monitoring": {
Type: schema.TypeBool,
Description: "Privilege for a group to access and manage monitoring related functionality " +
"(access metrics, CRUD on alarms, alarm-actions etc) using Monotoring-as-a-Service (MaaS).",
Computed: true,
},
"access_and_manage_certificates": {
Type: schema.TypeBool,
Description: "Privilege for a group to access and manage certificates.",
Computed: true,
},
"manage_dbaas": {
Type: schema.TypeBool,
Description: "Privilege for a group to manage DBaaS related functionality",
Computed: true,
},
"users": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"first_name": {
Type: schema.TypeString,
Computed: true,
},
"last_name": {
Type: schema.TypeString,
Computed: true,
},
"email": {
Type: schema.TypeString,
Computed: true,
},
"administrator": {
Type: schema.TypeBool,
Computed: true,
},
"force_sec_auth": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
Timeouts: &resourceDefaultTimeouts,
}
}
func dataSourceGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(services.SdkBundle).CloudApiClient
id, idOk := d.GetOk("id")
name, nameOk := d.GetOk("name")
if idOk && nameOk {
diags := diag.FromErr(errors.New("id and name cannot be both specified in the same time"))
return diags
}
if !idOk && !nameOk {
diags := diag.FromErr(errors.New("please provide either the group id or name"))
return diags
}
var group ionoscloud.Group
var err error
if idOk {
/* search by ID */
var apiResponse *ionoscloud.APIResponse
group, apiResponse, err = client.UserManagementApi.UmGroupsFindById(ctx, id.(string)).Execute()
logApiRequestTime(apiResponse)
if err != nil {
diags := diag.FromErr(fmt.Errorf("an error occurred while fetching group with ID %s: %w", id.(string), err))
return diags
}
} else {
/* search by name */
var groups ionoscloud.Groups
groups, apiResponse, err := client.UserManagementApi.UmGroupsGet(ctx).Depth(1).Execute()
logApiRequestTime(apiResponse)
if err != nil {
diags := diag.FromErr(fmt.Errorf("an error occurred while fetching groups: %w", err))
return diags
}
var results []ionoscloud.Group
if groups.Items != nil {
for _, g := range *groups.Items {
if g.Properties != nil && g.Properties.Name != nil && *g.Properties.Name == name.(string) {
/* group found */
group, apiResponse, err = client.UserManagementApi.UmGroupsFindById(ctx, *g.Id).Execute()
logApiRequestTime(apiResponse)
if err != nil {
diags := diag.FromErr(fmt.Errorf("an error occurred while fetching group %s: %w", *g.Id, err))
return diags
}
results = append(results, g)
}
}
}
if results == nil || len(results) == 0 {
return diag.FromErr(fmt.Errorf("no group found with the specified name = %s", name))
} else if len(results) > 1 {
return diag.FromErr(fmt.Errorf("more than one group found with the specified criteria name = %s", name))
} else {
group = results[0]
}
}
if err = setGroupData(ctx, client, d, &group); err != nil {
return diag.FromErr(err)
}
return nil
}