/
data_akamai_edgeworker_activation.go
88 lines (76 loc) · 2.59 KB
/
data_akamai_edgeworker_activation.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
package edgeworkers
import (
"context"
"fmt"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v2/pkg/edgeworkers"
"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"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func dataSourceEdgeWorkerActivation() *schema.Resource {
return &schema.Resource{
Description: "Fetch latest activation for given EdgeWorkerID",
ReadContext: dataEdgeWorkerActivationRead,
Schema: map[string]*schema.Schema{
"edgeworker_id": {
Type: schema.TypeInt,
Required: true,
Description: "The unique identifier of the EdgeWorker",
},
"network": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{
string(edgeworkers.ActivationNetworkStaging),
string(edgeworkers.ActivationNetworkProduction),
}, false)),
Description: "The network from which the activation will be fetched",
},
"activation_id": {
Type: schema.TypeInt,
Computed: true,
Description: "A unique identifier of the activation",
},
"version": {
Type: schema.TypeString,
Computed: true,
Description: "The version of EdgeWorker",
},
},
}
}
func dataEdgeWorkerActivationRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
meta := akamai.Meta(m)
logger := meta.Log("EdgeWorkers", "dataEdgeWorkerActivationsRead")
ctx = session.ContextWithOptions(
ctx,
session.WithContextLog(logger),
)
client := inst.Client(meta)
logger.Debug("Reading EdgeWorker Activations")
edgeworkerID, err := tools.GetIntValue("edgeworker_id", d)
if err != nil {
return diag.Errorf("could not get edgeworker_id: %s", err)
}
network, err := tools.GetStringValue("network", d)
if err != nil {
return diag.Errorf("could not get network: %s", err)
}
activation, err := getCurrentActivation(ctx, client, edgeworkerID, network, false)
if err != nil {
return diag.Errorf("could not get current activation: %s", err)
}
if activation != nil {
if err = d.Set("activation_id", activation.ActivationID); err != nil {
return diag.Errorf("could not set activation_id: %s", err)
}
if err = d.Set("version", activation.Version); err != nil {
return diag.Errorf("could not set version: %s", err)
}
}
d.SetId(fmt.Sprintf("%d:%s", edgeworkerID, network))
return nil
}