/
data_iam_reference_action.go
82 lines (73 loc) · 1.8 KB
/
data_iam_reference_action.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
package ovh
import (
"context"
"log"
"net/url"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceIamReferenceActions() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIamReferenceActionRead,
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
},
"actions": {
Type: schema.TypeSet,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"action": {
Type: schema.TypeString,
Computed: true,
},
"categories": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
},
},
},
Computed: true,
},
},
}
}
func dataSourceIamReferenceActionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
resType := d.Get("type").(string)
e, err := url.Parse("/v2/iam/reference/action")
if err != nil {
return diag.Errorf("Unable to get actions:\n\t %q", err)
}
q := e.Query()
q.Add("resourceType", resType)
e.RawQuery = q.Encode()
log.Printf("[DEBUG] actions url: %+s", e.String())
var actions []IamReferenceAction
err = config.OVHClient.GetWithContext(
ctx,
e.String(),
&actions,
)
if err != nil {
return diag.Errorf("Unable to get actions:\n\t %q", err)
}
log.Printf("[DEBUG] actions: %+v", actions)
saveAct := make([]map[string]any, 0, len(actions))
for _, a := range actions {
saveAct = append(saveAct, a.ToMap())
}
d.SetId(resType)
d.Set("actions", saveAct)
return nil
}