-
Notifications
You must be signed in to change notification settings - Fork 98
/
data_source_aci_cloudtemplateextnetwork.go
109 lines (98 loc) · 2.96 KB
/
data_source_aci_cloudtemplateextnetwork.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
package aci
import (
"context"
"fmt"
"log"
"github.com/ciscoecosystem/aci-go-client/v2/client"
"github.com/ciscoecosystem/aci-go-client/v2/models"
"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 dataSourceAciCloudTemplateforExternalNetwork() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceAciCloudTemplateforExternalNetworkRead,
SchemaVersion: 1,
Schema: AppendBaseAttrSchema(AppendNameAliasAttrSchema(map[string]*schema.Schema{
"hub_network_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"vrf_dn": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"regions": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Computed: true,
},
"cloud_vendor": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"aws",
"azure",
"gcp",
}, false),
},
"router_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"c8kv",
"tgw",
}, false),
},
})),
}
}
func dataSourceAciCloudTemplateforExternalNetworkRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
aciClient := m.(*client.Client)
name := d.Get("name").(string)
rn := fmt.Sprintf(models.RncloudtemplateExtNetwork, name)
dn := fmt.Sprintf("%s/%s", models.CloudInfraNetworkDefaultTemplateDn, rn)
log.Printf("[DEBUG] %s: Data Source - Beginning Read", dn)
cloudtemplateExtNetwork, err := getRemoteCloudTemplateforExternalNetwork(aciClient, dn)
if err != nil {
return diag.FromErr(err)
}
d.SetId(dn)
_, err = setCloudTemplateforExternalNetworkAttributes(cloudtemplateExtNetwork, d)
if err != nil {
return diag.FromErr(err)
}
log.Printf("[DEBUG] : Data Source - Begining Read of cloud Regions attributes.")
regionsData, err := aciClient.ListCloudProviderandRegionNames(cloudtemplateExtNetwork.DistinguishedName)
if err != nil {
log.Printf("[DEBUG] : Data Source - Error while reading cloud Regions attributes %v", err)
}
regionsList := make([]string, 0, 1)
for _, regionValue := range regionsData {
regionsMap, err := setCloudProviderandRegionNamesAttributes(regionValue, make(map[string]string))
if err != nil {
d.SetId("")
return nil
}
regionsList = append(regionsList, regionsMap["region"])
d.Set("cloud_vendor", regionsMap["cloud_vendor"])
if regionsMap["cloud_vendor"] != "aws" {
d.Set("router_type", "")
}
}
log.Printf("[DEBUG] : Data Source - Read cloud regions finished successfully")
d.Set("regions", regionsList)
log.Printf("[DEBUG] %s: Data Source - Read finished successfully", dn)
return nil
}