-
Notifications
You must be signed in to change notification settings - Fork 3
/
datasource_service_template.go
119 lines (101 loc) · 3.27 KB
/
datasource_service_template.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
116
117
118
119
package cloudforms
import (
"encoding/json"
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceServiceTemplate() *schema.Resource {
return &schema.Resource{
Read: dataSourceServiceTemplateRead,
Schema: map[string]*schema.Schema{
// required values
"name": {
Type: schema.TypeString,
Required: true,
},
// computed values
"href": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"service_template_catalog_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
// get catalogID
var serviceTemplateCatalogID string
// Get templateID to fetch Service_templates associated with it
var tmpID string
// structure to store template detail
var templateDetailstruct TemplateQuery
// dataSourceServiceTemplateRead performs service_template lookup
func dataSourceServiceTemplateRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(Config)
// Get index of template
var index int
templateName := d.Get("name").(string)
if templateName == "" {
return fmt.Errorf("name of template must be set for data source service_template")
}
log.Println("[DEBUG] Reading Service templates...")
response, err := GetServiceTemplate(config, templateName)
if err != nil {
log.Printf("[ERROR] Error while getting response: %s", err)
return fmt.Errorf("Error while getting response: %s", err)
}
// store service template
if json.Unmarshal(response, &templateDetailstruct); err != nil {
log.Printf("[Error] Error while unmarshalling json: %s", err)
return fmt.Errorf("Error while unmarshalling json: %s", err)
}
// subcount is nothing but number of successful results
if templateDetailstruct.Subcount == 0 {
log.Printf("[DEBUG] Template called `%s` Not found in List ", templateName)
return fmt.Errorf("Template called `%s` Not found in List", templateName)
}
// index of template from result
for i := 0; i < templateDetailstruct.Subcount; i++ {
if templateDetailstruct.Resources[i].Name == templateName {
index = i
tmpID = templateDetailstruct.Resources[i].ID
log.Printf("[DEBUG] Template called `%s` found in List ", templateName)
break
}
}
// Set values into schema
d.Set("href", templateDetailstruct.Resources[index].Href)
d.Set("id", templateDetailstruct.Resources[index].ID)
d.Set("name", templateDetailstruct.Resources[index].Name)
d.Set("description", templateDetailstruct.Resources[index].Description)
d.Set("tenant_id", templateDetailstruct.Resources[index].TenantID)
d.Set("type", templateDetailstruct.Resources[index].Type)
d.Set("service_template_catalog_id", templateDetailstruct.Resources[index].ServiceTemplateCatalogID)
// Calling SetId on our schema.ResourceData using a value suitable for resource.
// This ensures whatever resource state we set on schema.ResourceData will be persisted in local state.
// If we neglect to SetId, no resource state will be persisted.
d.SetId(fmt.Sprintf("%s", tmpID))
return nil
}