generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
/
data_source_policyset.go
105 lines (93 loc) · 2.76 KB
/
data_source_policyset.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
package policyset
import (
"context"
"errors"
"net/http"
"github.com/antihax/optional"
"github.com/harness/harness-go-sdk/harness/policymgmt"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourcePolicyset() *schema.Resource {
resource := &schema.Resource{
Description: "Data source for retrieving a Harness policyset.",
ReadContext: dataSourceProjectRead,
Schema: map[string]*schema.Schema{
"action": {
Description: "Action code for the policyset.",
Type: schema.TypeString,
Optional: false,
Required: true,
Computed: false,
},
"type": {
Description: "Type of the policyset.",
Type: schema.TypeString,
Optional: false,
Required: true,
Computed: false,
},
"enabled": {
Description: "Enabled for the policyset.",
Type: schema.TypeBool,
Optional: true,
Required: false,
Computed: false,
},
"policies": {
Description: "List of policy identifiers / severity for the policyset.",
Type: schema.TypeList,
Computed: false,
Optional: true,
Required: false,
MinItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"identifier": {
Description: "Account Identifier of the account",
Type: schema.TypeString,
Optional: false,
Required: true,
},
"severity": {
Description: "Policy failure response - 'warning' for continuation, 'error' for exit",
Type: schema.TypeString,
Optional: false,
Required: true,
},
},
},
},
},
}
helpers.SetMultiLevelDatasourceSchemaIdentifierRequired(resource.Schema)
return resource
}
func dataSourceProjectRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).GetPolicyManagementClient()
id := d.Get("identifier").(string)
var err error
var policyset policymgmt.PolicySet2
var httpResp *http.Response
if id != "" {
policyset, _, _ = c.PolicysetsApi.PolicysetsFind(ctx, id, &policymgmt.PolicysetsApiPolicysetsFindOpts{
AccountIdentifier: optional.NewString(meta.(*internal.Session).AccountId),
XApiKey: optional.NewString(meta.(*internal.Session).PLClient.ApiKey),
})
} else {
return diag.FromErr(errors.New("identifier must be specified"))
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
bla := policymgmt.PolicySet2{}
if policyset.Identifier == bla.Identifier {
d.SetId("")
d.MarkNewResource()
return nil
}
readPolicyset(d, policyset)
return nil
}