/
resource_deploy_policy_set.go
146 lines (119 loc) · 4.13 KB
/
resource_deploy_policy_set.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
package nirmata
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/nirmata/go-client/pkg/client"
)
func resourceDeployPolicySet() *schema.Resource {
return &schema.Resource{
Create: resourceDeployPolicySetCreate,
Read: resourceDeployPolicySetRead,
Update: resourceDeployPolicySetUpdate,
Delete: resourceDeployPolicySetDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"policy_set_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateName,
},
"cluster": {
Type: schema.TypeString,
Required: true,
},
"delete_from_cluster": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
func resourceDeployPolicySetCreate(d *schema.ResourceData, meta interface{}) error {
apiClient := meta.(client.Client)
nameOrID := d.Get("policy_set_name").(string)
clusterNameOrID := d.Get("cluster").(string)
clusterID, err := fetchID(apiClient, client.ServiceClusters, "KubernetesCluster", clusterNameOrID)
if err != nil {
log.Printf("[ERROR] - failed to resolve cluster %s %v", clusterNameOrID, err)
return err
}
groupID, err := fetchID(apiClient, client.ServiceClusters, "PolicyGroup", nameOrID)
if err != nil {
log.Printf("[ERROR] - failed to resolve policy group %s %v", clusterNameOrID, err)
return err
}
clusterRef := map[string]interface{}{
"id": clusterID.UUID(),
"service": "Cluster",
"modelIndex": "KubernetesCluster",
}
data := map[string]interface{}{
"name": clusterNameOrID,
"parent": groupID.UUID(),
"clusterRef": clusterRef,
}
log.Printf("[DEBUG] - deploying policy group %s with %+v", nameOrID, data)
deployData, err := apiClient.PostFromJSON(client.ServiceClusters, "PolicyGroupCluster", data, nil)
if err != nil {
log.Printf("[ERROR] - failed to deploy policy group %s with data %v: %v", nameOrID, data, err)
return err
}
deployDataUUID := deployData["id"].(string)
d.SetId(deployDataUUID)
policyGroupClusterID := client.NewID(client.ServiceClusters, "PolicyGroupCluster", deployData["id"].(string))
state, waitErr := waitForPolicyDeploySetSyncStatus(apiClient, d.Timeout(schema.TimeoutCreate), policyGroupClusterID)
if waitErr != nil {
log.Printf("[ERROR] - failed to deploy policy group sync status. Error - %v", waitErr)
return waitErr
}
if strings.EqualFold("failed", state) {
status, err := getPolicyDeployGroupStatus(apiClient, policyGroupClusterID)
if err != nil {
log.Printf("[ERROR] - failed to retrieve policy group sync details: %v", err)
return fmt.Errorf(" [ERROR] - policy group sync failed")
}
return fmt.Errorf(" [ERROR] - policy group sync failed: %s", status)
}
log.Printf("[INFO] - policy group deploy successfully %s %s", nameOrID, deployDataUUID)
return nil
}
func resourceDeployPolicySetRead(d *schema.ResourceData, meta interface{}) error {
apiClient := meta.(client.Client)
name := d.Get("cluster").(string)
id := client.NewID(client.ServiceClusters, "PolicyGroupCluster", d.Id())
_, err := apiClient.Get(id, &client.GetOptions{})
if err != nil {
if strings.Contains(err.Error(), "404") {
log.Printf("[INFO] deployed policy group %+v not found", id.Map())
d.SetId("")
return nil
}
log.Printf("[ERROR] failed to retrieve deployed policy group details %s (%s): %v", name, id, err)
return err
}
log.Printf("[INFO] - retrieved policy group %s %s", name, id.UUID())
return nil
}
func resourceDeployPolicySetUpdate(d *schema.ResourceData, meta interface{}) error {
return nil
}
func resourceDeployPolicySetDelete(d *schema.ResourceData, meta interface{}) error {
apiClient := meta.(client.Client)
name := d.Get("cluster").(string)
deleteFromCluster := d.Get("delete_from_cluster").(bool)
id := client.NewID(client.ServiceClusters, "PolicyGroupCluster", d.Id())
params := map[string]string{
"deleteFromCluster": strconv.FormatBool(deleteFromCluster),
}
if err := apiClient.Delete(id, params); err != nil {
return err
}
log.Printf("[INFO] - deleted policy group %s %s", name, id.UUID())
return nil
}