/
data_source_project_role.go
113 lines (97 loc) · 2.4 KB
/
data_source_project_role.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
/*
Use this data source to list project roles for a speficied project.
Example Usage
```hcl
resource "awx_project" "myproj" {
name = "My AWX Project"
}
data "awx_project_role" "proj_admins" {
name = "Admin"
project_id = resource.awx_project.myproj.id
}
```
*/
package awx
import (
"context"
"strconv"
awx "github.com/denouche/goawx/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceProjectRole() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceProjectRolesRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeInt,
Required: true,
},
"id": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func dataSourceProjectRolesRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*awx.AWX)
params := make(map[string]string)
projectId := d.Get("project_id").(int)
if projectId == 0 {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Get: Missing Parameters",
Detail: "project_id parameter is required.",
})
return diags
}
project, err := client.ProjectService.GetProjectByID(projectId, params)
if err != nil {
return buildDiagnosticsMessage(
"Get: Fail to fetch Project",
"Fail to find the project got: %s",
err.Error(),
)
}
roleslist := []*awx.ApplyRole{
project.SummaryFields.ObjectRoles.UseRole,
project.SummaryFields.ObjectRoles.AdminRole,
project.SummaryFields.ObjectRoles.UpdateRole,
project.SummaryFields.ObjectRoles.ReadRole,
}
if roleID, okID := d.GetOk("id"); okID {
id := roleID.(int)
for _, v := range roleslist {
if v != nil && id == v.ID {
d = setProjectRoleData(d, v)
return diags
}
}
}
if roleName, okName := d.GetOk("name"); okName {
name := roleName.(string)
for _, v := range roleslist {
if v != nil && name == v.Name {
d = setProjectRoleData(d, v)
return diags
}
}
}
return buildDiagnosticsMessage(
"Failed to fetch project role - Not Found",
"The project role was not found",
)
}
func setProjectRoleData(d *schema.ResourceData, r *awx.ApplyRole) *schema.ResourceData {
d.Set("name", r.Name)
d.SetId(strconv.Itoa(r.ID))
return d
}