-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_destination_rule.go
149 lines (136 loc) · 4.49 KB
/
resource_destination_rule.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
package istio
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"istio.io/client-go/pkg/apis/networking/v1alpha3"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
pkgApi "k8s.io/apimachinery/pkg/types"
)
func resourceDestinationRule() *schema.Resource {
return &schema.Resource{
Create: resourceDestinationRuleCreate,
Read: resourceDestinationRuleRead,
Exists: resourceDestinationRuleExists,
Update: resourceDestinationRuleUpdate,
Delete: resourceDestinationRuleDelete,
Schema: map[string]*schema.Schema{
"metadata": namespacedMetadataSchema("destinationrule"),
"spec": {
Type: schema.TypeList,
Description: "Spec defines the specification of the DestinationRule",
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: destinationRuleSpecFields(),
},
},
},
}
}
func resourceDestinationRuleCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Creating New DestinationRule resourceDestinationRuleCreate")
ic := meta.(*Config).istioClientset
metadata := expandMetadata(d.Get("metadata").([]interface{}))
spec, err := expandDestinationRulesSpec(d.Get("spec").([]interface{}))
if err != nil {
return err
}
dr := v1alpha3.DestinationRule{
ObjectMeta: metadata,
Spec: *spec,
}
out, err := ic.NetworkingV1alpha3().DestinationRules(metadata.Namespace).Create(context.TODO(), &dr, metav1.CreateOptions{})
if err != nil {
log.Printf("[DEBUG] Failed to create DestinationRule: %#v", err)
}
d.SetId(buildID(out.ObjectMeta))
// Add wait time of creation
time.Sleep(5 * time.Second)
log.Printf("[INFO] Created New DestinationRule %#v", out)
return resourceDestinationRuleRead(d, meta)
}
func resourceDestinationRuleRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Read DestinationRule resourceDestinationRuleRead")
ic := meta.(*Config).istioClientset
namespace, name, err := idParts(d.Id())
if err != nil {
return err
}
dr, err := ic.NetworkingV1alpha3().DestinationRules(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
log.Printf("[DEBUG] Received error: %#v", err)
}
err = d.Set("metadata", flattenMetadata(dr.ObjectMeta, d))
if err != nil {
return err
}
return nil
}
func resourceDestinationRuleExists(d *schema.ResourceData, meta interface{}) (bool, error) {
log.Printf("[INFO] Exists DestinationRule resourceDestinationRuleExists")
ic := meta.(*Config).istioClientset
namespace, name, err := idParts(d.Id())
if err != nil {
return false, err
}
_, err = ic.NetworkingV1alpha3().DestinationRules(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
if statusErr, ok := err.(*errors.StatusError); ok && statusErr.ErrStatus.Code == 404 {
return false, nil
}
log.Printf("[DEBUG] Received error: %#v", err)
}
return true, nil
}
func resourceDestinationRuleUpdate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] update DestinationRule resourceDestinationRuleUpdate")
ic := meta.(*Config).istioClientset
namespace, name, err := idParts(d.Id())
if err != nil {
return err
}
ops := patchMetadata("metadata.0.", "/metadata/", d)
if d.HasChange("spec") {
spec, err := expandDestinationRulesSpec(d.Get("spec").([]interface{}))
if err != nil {
return err
}
ops = append(ops, &ReplaceOperation{
Path: "/spec",
Value: spec,
})
}
data, err := ops.MarshalJSON()
if err != nil {
return fmt.Errorf("Failed to marshal update operations: %s", err)
}
out, err := ic.NetworkingV1alpha3().DestinationRules(namespace).Patch(context.TODO(), name, pkgApi.JSONPatchType, data, metav1.PatchOptions{})
if err != nil {
log.Printf("[DEBUG] Received error: %#v", err)
}
log.Printf("[INFO] Submitted updated DestinationRule: %#v", out)
// Add wait time of creation
time.Sleep(5 * time.Second)
return resourceDestinationRuleRead(d, meta)
}
func resourceDestinationRuleDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] delete DestinationRule resourceDestinationRuleDelete")
ic := meta.(*Config).istioClientset
namespace, name, err := idParts(d.Id())
if err != nil {
return err
}
err = ic.NetworkingV1alpha3().DestinationRules(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{})
if err != nil {
log.Printf("[DEBUG] DestinationRule delete error: %#v", err)
}
log.Printf("[INFO] DestinationRule deleted")
d.SetId("")
// Add wait time of creation
time.Sleep(5 * time.Second)
return nil
}