/
data_property_akamai_contract.go
102 lines (89 loc) · 2.99 KB
/
data_property_akamai_contract.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
package property
import (
"context"
"errors"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v2/pkg/session"
"github.com/akamai/terraform-provider-akamai/v2/pkg/akamai"
"github.com/akamai/terraform-provider-akamai/v2/pkg/tools"
)
func dataSourcePropertyContract() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourcePropertyContractRead,
Schema: map[string]*schema.Schema{
"group": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"group", "group_id", "group_name"},
Deprecated: akamai.NoticeDeprecatedUseAlias("group"),
},
"group_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"group", "group_id", "group_name"},
},
"group_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"group", "group_id", "group_name"},
},
},
}
}
func dataSourcePropertyContractRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
meta := akamai.Meta(m)
log := meta.Log("PAPI", "dataSourcePropertyContractRead")
// create a context with logging for api calls
ctx = session.ContextWithOptions(
ctx,
session.WithContextLog(log),
)
// check if one of group_id/group_name exists.
group, err := tools.ResolveKeyStringState(d, "group_id", "group_name")
if err != nil {
// if both group_id/group_name not present in the state, check for group.
group, err = tools.GetStringValue("group", d)
// If no group found, just return the first contract
if err != nil {
if !errors.Is(err, tools.ErrNotFound) {
return diag.FromErr(err)
}
contracts, err := inst.Client(meta).GetContracts(ctx)
if err != nil {
return diag.Errorf("error looking up Contracts for group %v: %s", group, err)
}
if len(contracts.Contracts.Items) == 0 {
return diag.Errorf("%v", ErrNoContractsFound)
}
d.SetId(contracts.Contracts.Items[0].ContractID)
return nil
}
}
// Otherwise find the group and return it's first contract
log.Debug("[Akamai Property Contract] Start Searching for property contract by group")
groups, err := getGroups(ctx, meta)
if err != nil {
return diag.FromErr(err)
}
for _, g := range groups.Groups.Items {
if g.GroupID != group && g.GroupID != tools.AddPrefix(group, "grp_") && g.GroupName != group {
continue
}
if len(g.ContractIDs) == 0 {
return diag.Errorf("%v: %v", ErrLookingUpContract, group)
}
// set group_id/group_name/group in state.
if err := d.Set("group_id", tools.AddPrefix(g.GroupID, "grp_")); err != nil {
return diag.Errorf("%v: %s", tools.ErrValueSet, err.Error())
}
if err := d.Set("group_name", g.GroupName); err != nil {
return diag.Errorf("%v: %s", tools.ErrValueSet, err.Error())
}
d.SetId(g.ContractIDs[0])
return nil
}
return diag.Errorf("%v; groupID: %v", ErrNoContractsFound, group)
}