/
data_akamai_property_products.go
83 lines (68 loc) · 2.37 KB
/
data_akamai_property_products.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
package property
import (
"context"
"encoding/json"
"fmt"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/papi"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/session"
"github.com/akamai/terraform-provider-akamai/v6/pkg/common/hash"
"github.com/akamai/terraform-provider-akamai/v6/pkg/common/tf"
"github.com/akamai/terraform-provider-akamai/v6/pkg/meta"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourcePropertyProducts() *schema.Resource {
return &schema.Resource{
ReadContext: dataPropertyProductsRead,
Schema: map[string]*schema.Schema{
"contract_id": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: tf.IsNotBlank,
},
"products": {
Type: schema.TypeList,
Computed: true,
Description: "List of products",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"product_name": {Type: schema.TypeString, Computed: true},
"product_id": {Type: schema.TypeString, Computed: true},
},
},
},
},
}
}
func dataPropertyProductsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
meta := meta.Must(m)
logger := meta.Log("PAPI", "dataPropertyProductsRead")
// create context with logging
ctx = session.ContextWithOptions(ctx, session.WithContextLog(logger))
client := Client(meta)
contractID, err := tf.GetStringValue("contract_id", d)
if err != nil {
return diag.FromErr(err) // fixme kind of error
}
logger.Debugf("[Akamai Property Products] Start searching for product records")
prdResp, err := client.GetProducts(ctx, papi.GetProductsRequest{ContractID: contractID})
if err != nil {
return diag.FromErr(err) // fixme kind of error
}
products := make([]map[string]string, 0, len(prdResp.Products.Items))
for _, prd := range prdResp.Products.Items {
product := map[string]string{
"product_name": prd.ProductName, "product_id": prd.ProductID}
products = append(products, product)
}
if err := d.Set("products", products); err != nil {
return diag.FromErr(fmt.Errorf("%w: %q", tf.ErrValueSet, err.Error()))
}
jsonBody, err := json.Marshal(products)
if err != nil {
return diag.FromErr(err)
}
d.SetId(hash.GetSHAString(string(jsonBody)))
logger.Debugf("[Akamai Property Products] Start searching for product records")
return nil
}