/
resource_akamai_property_common.go
131 lines (119 loc) · 3.71 KB
/
resource_akamai_property_common.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
package property
// This file contains functions removed from resource_akamai_property.go that are still referenced elsewhere
import (
"context"
"fmt"
"strconv"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v6/pkg/papi"
"github.com/akamai/terraform-provider-akamai/v4/pkg/akamai"
"github.com/akamai/terraform-provider-akamai/v4/pkg/tools"
)
func getGroup(ctx context.Context, meta akamai.OperationMeta, groupID string) (*papi.Group, error) {
logger := meta.Log("PAPI", "getGroup")
client := inst.Client(meta)
logger.Debugf("Fetching groups")
res, err := client.GetGroups(ctx)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrFetchingGroups, err.Error())
}
groupID = tools.AddPrefix(groupID, "grp_")
var group *papi.Group
var groupFound bool
for _, g := range res.Groups.Items {
if g.GroupID == groupID {
group = g
groupFound = true
break
}
}
if !groupFound {
return nil, fmt.Errorf("%w: %s", ErrGroupNotFound, groupID)
}
logger.Debugf("Group found: %s", group.GroupID)
return group, nil
}
func getContract(ctx context.Context, meta akamai.OperationMeta, contractID string) (*papi.Contract, error) {
logger := meta.Log("PAPI", "getContract")
client := inst.Client(meta)
logger.Debugf("Fetching contract")
res, err := client.GetContracts(ctx)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrFetchingContracts, err.Error())
}
var contract *papi.Contract
var contractFound bool
for _, c := range res.Contracts.Items {
if c.ContractID == contractID {
contract = c
contractFound = true
break
}
}
if !contractFound {
return nil, fmt.Errorf("%w: %s", ErrContractNotFound, contractID)
}
logger.Debugf("Contract found: %s", contract.ContractID)
return contract, nil
}
func getProduct(ctx context.Context, meta akamai.OperationMeta, productID, contractID string) (*papi.ProductItem, error) {
logger := meta.Log("PAPI", "getProduct")
client := inst.Client(meta)
if contractID == "" {
return nil, ErrNoContractProvided
}
logger.Debugf("Fetching product")
res, err := client.GetProducts(ctx, papi.GetProductsRequest{ContractID: contractID})
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrProductFetch, err.Error())
}
var productFound bool
var product papi.ProductItem
for _, p := range res.Products.Items {
if p.ProductID == productID {
product = p
productFound = true
break
}
}
if !productFound {
return nil, fmt.Errorf("%w: %s", ErrProductNotFound, productID)
}
logger.Debugf("Product found: %s", product.ProductID)
return &product, nil
}
func convertString(v string) interface{} {
if f1, err := strconv.ParseFloat(v, 64); err == nil {
return f1
}
// FIXME: execution will never reach this as every int representation will be captured by ParseFloat() above
// this should either be moved above ParseFloat block or removed
if f2, err := strconv.ParseInt(v, 10, 64); err == nil {
return f2
}
if f3, err := strconv.ParseBool(v); err == nil {
return f3
}
return v
}
func findProperty(ctx context.Context, name string, meta akamai.OperationMeta) (*papi.Property, error) {
client := inst.Client(meta)
results, err := client.SearchProperties(ctx, papi.SearchRequest{Key: papi.SearchKeyPropertyName, Value: name})
if err != nil {
return nil, err
}
if len(results.Versions.Items) == 0 {
return nil, fmt.Errorf("%w: %s", ErrPropertyNotFound, name)
}
property, err := client.GetProperty(ctx, papi.GetPropertyRequest{
ContractID: results.Versions.Items[0].ContractID,
GroupID: results.Versions.Items[0].GroupID,
PropertyID: results.Versions.Items[0].PropertyID,
})
if err != nil {
return nil, err
}
if len(property.Properties.Items) == 0 {
return nil, fmt.Errorf("%w: %s", ErrPropertyNotFound, name)
}
return property.Properties.Items[0], nil
}