-
Notifications
You must be signed in to change notification settings - Fork 117
/
datasource_bigip_ltm_datagroup.go
89 lines (85 loc) · 2.45 KB
/
datasource_bigip_ltm_datagroup.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
/*
Copyright 2019 F5 Networks Inc.
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package bigip
import (
"context"
"fmt"
"log"
bigip "github.com/f5devcentral/go-bigip"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceBigipLtmDataGroup() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBigipLtmDataGroupRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the Data Group List",
},
"partition": {
Type: schema.TypeString,
Required: true,
Description: "partition of resource group",
},
"type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The Data Group type (string, ip, integer)",
},
"record": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"data": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
}
}
func dataSourceBigipLtmDataGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*bigip.BigIP)
d.SetId("")
var records []map[string]interface{}
name := fmt.Sprintf("/%s/%s", d.Get("partition").(string), d.Get("name").(string))
log.Printf("[INFO] Retrieving Data Group List %s", name)
dataGroup, err := client.GetInternalDataGroup(name)
if err != nil {
return diag.FromErr(fmt.Errorf("error retrieving Data Group List %s: %v ", name, err))
}
if dataGroup == nil {
log.Printf("[DEBUG] Data Group List %s not found, removing from state", name)
d.SetId("")
return nil
}
_ = d.Set("name", dataGroup.Name)
_ = d.Set("partition", dataGroup.Partition)
_ = d.Set("type", dataGroup.Type)
for _, record := range dataGroup.Records {
dgRecord := map[string]interface{}{
"name": record.Name,
"data": record.Data,
}
records = append(records, dgRecord)
}
if err := d.Set("record", records); err != nil {
return diag.FromErr(fmt.Errorf("error updating records in state for Data Group List %s: %v ", name, err))
}
d.SetId(dataGroup.FullPath)
return nil
}