/
data_akamai_properties_search.go
115 lines (100 loc) · 3.76 KB
/
data_akamai_properties_search.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
package property
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/papi"
"github.com/akamai/terraform-provider-akamai/v6/pkg/common/tf"
"github.com/akamai/terraform-provider-akamai/v6/pkg/meta"
)
func dataSourcePropertiesSearch() *schema.Resource {
return &schema.Resource{
ReadContext: dataPropertiesSearchRead,
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
Description: "Key must have one of three values: 'edgeHostname', 'hostname' or 'propertyName'",
ValidateDiagFunc: tf.ValidateStringInSlice([]string{papi.SearchKeyEdgeHostname, papi.SearchKeyHostname, papi.SearchKeyPropertyName}),
},
"value": {
Type: schema.TypeString,
Required: true,
Description: "Value of Search",
ValidateDiagFunc: tf.IsNotBlank,
},
"properties": {
Type: schema.TypeList,
Computed: true,
Description: "List of properties",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"account_id": {Type: schema.TypeString, Computed: true},
"asset_id": {Type: schema.TypeString, Computed: true},
"contract_id": {Type: schema.TypeString, Computed: true},
"group_id": {Type: schema.TypeString, Computed: true},
"property_id": {Type: schema.TypeString, Computed: true},
"property_version": {Type: schema.TypeInt, Computed: true},
"property_name": {Type: schema.TypeString, Computed: true},
"edge_hostname": {Type: schema.TypeString, Computed: true},
"hostname": {Type: schema.TypeString, Computed: true},
"production_status": {Type: schema.TypeString, Computed: true},
"staging_status": {Type: schema.TypeString, Computed: true},
"updated_by_user": {Type: schema.TypeString, Computed: true},
"updated_date": {Type: schema.TypeString, Computed: true},
},
},
},
},
}
}
func dataPropertiesSearchRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
meta := meta.Must(m)
client := Client(meta)
log := meta.Log("PAPI", "dataPropertiesSearchRead")
log.Debug("Searching properties")
var key, value string
var err error
if key, err = tf.GetStringValue("key", d); err != nil {
return diag.FromErr(err)
}
if value, err = tf.GetStringValue("value", d); err != nil {
return diag.FromErr(err)
}
request := papi.SearchRequest{
Key: key,
Value: value,
}
search, err := client.SearchProperties(ctx, request)
if err != nil {
return diag.Errorf("could not load properties: %s", err)
}
d.SetId(fmt.Sprintf("%s:%s", request.Key, request.Value))
if err := d.Set("properties", sliceResponseSearch(search)); err != nil {
return diag.Errorf("error setting properties: %s", err)
}
return nil
}
func sliceResponseSearch(propertiesResponse *papi.SearchResponse) []map[string]interface{} {
var properties []map[string]interface{}
for _, item := range propertiesResponse.Versions.Items {
property := map[string]interface{}{
"account_id": item.AccountID,
"asset_id": item.AssetID,
"contract_id": item.ContractID,
"group_id": item.GroupID,
"property_id": item.PropertyID,
"property_version": item.PropertyVersion,
"property_name": item.PropertyName,
"edge_hostname": item.EdgeHostname,
"hostname": item.Hostname,
"production_status": item.ProductionStatus,
"staging_status": item.StagingStatus,
"updated_by_user": item.UpdatedByUser,
"updated_date": item.UpdatedDate,
}
properties = append(properties, property)
}
return properties
}