generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers.go
117 lines (105 loc) · 2.36 KB
/
helpers.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
package client
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
type Tag struct {
Name string `json:"name"`
Value string `json:"value"`
}
func NewTags(d *schema.ResourceData) []Tag {
rawTags := d.Get("tags").(map[string]interface{})
tags := make([]Tag, len(rawTags))
index := 0
for key, value := range rawTags {
t := Tag{
Name: key,
Value: value.(string),
}
tags[index] = t
index += 1
}
return tags
}
func AssignTagsToResource(ctx context.Context, c *Client, rID, rName string, tags []Tag) error {
body, err := json.Marshal(tags)
if err != nil {
return fmt.Errorf("could not convert tags to json: %v", err)
}
url := fmt.Sprintf("%s/v1/%s/%s/tags", c.BaseURL, rName, rID)
_, err = c.Put(ctx, url, body)
if err != nil {
return err
}
return nil
}
func MapResponseToResource(r interface{}, d *schema.ResourceData, excludedKeys []string) error {
var interfaceResponse map[string]interface{}
marshaledResponse, err := json.Marshal(r)
if err != nil {
return err
}
err = json.Unmarshal(marshaledResponse, &interfaceResponse)
if err != nil {
return err
}
for key, val := range interfaceResponse {
if isKeyExluded(key, excludedKeys) {
continue
}
err = d.Set(key, val)
if err != nil {
return err
}
}
return nil
}
func isKeyExluded(key string, excludedKeys []string) bool {
for _, excludedKey := range excludedKeys {
if key == excludedKey {
return true
}
}
return false
}
func ResourceTypeSetToStringSlice(s *schema.Set) []string {
valuesList := s.List()
values := make([]string, len(valuesList))
for i := 0; i < len(valuesList); i++ {
values[i] = fmt.Sprint(valuesList[i])
}
return values
}
func ConfigToStringSlice(key string, d *schema.ResourceData) []string {
data := d.Get(key).([]interface{})
res := make([]string, len(data))
for i, val := range data {
res[i] = val.(string)
}
return res
}
func ConvertTagsListToMap(tags []Tag) map[string]string {
res := make(map[string]string)
for _, tag := range tags {
res[tag.Name] = tag.Value
}
return res
}
func Contains(v string, a []string) bool {
for _, i := range a {
if i == v {
return true
}
}
return false
}
func networkElementPathByPrefix(neID string) string {
prefix := strings.Split(neID, "-")[0]
if prefix == "dev" {
return devicesEndpoint
}
return networkElementsEndpoint
}