/
data_source_ibm_schematics_output.go
103 lines (87 loc) · 2.29 KB
/
data_source_ibm_schematics_output.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
package ibm
import (
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceSchematicsOut() *schema.Resource {
return &schema.Resource{
Read: resourceIBMSchematicsOutRead,
Schema: map[string]*schema.Schema{
"workspace_id": {
Type: schema.TypeString,
Required: true,
Description: "The id of workspace",
},
"template_id": {
Type: schema.TypeString,
Required: true,
Description: "The id of template",
},
"type": {
Type: schema.TypeInt,
Computed: true,
},
"output_values": {
Type: schema.TypeMap,
Computed: true,
},
"output_json": {
Type: schema.TypeString,
Optional: true,
Description: "The json output in string",
},
ResourceControllerURL: {
Type: schema.TypeString,
Computed: true,
Description: "The URL of the IBM Cloud dashboard that can be used to explore and view details about this Workspace",
},
},
}
}
func resourceIBMSchematicsOutRead(d *schema.ResourceData, meta interface{}) error {
scClient, err := meta.(ClientSession).SchematicsAPI()
if err != nil {
return err
}
wrkAPI := scClient.Workspaces()
workspaceID := d.Get("workspace_id").(string)
templateID := d.Get("template_id").(string)
out, err := wrkAPI.GetOutputValues(workspaceID)
if err != nil {
return fmt.Errorf("Error while retreiving outputs of workspace: %s", err)
}
var outputJSON string
items := make(map[string]interface{})
found := false
for _, feilds := range out {
if feilds.TemplateID == templateID {
output := feilds.Output
found = true
outputByte, err := json.MarshalIndent(output, "", "")
if err != nil {
return err
}
outputJSON = string(outputByte[:])
// items := map[string]interface{}
for _, value := range output {
for key, val := range value {
val2 := val.Value
items[key] = val2
}
}
}
}
if !(found) {
return fmt.Errorf("Error while fetching template id in workspace: %s", workspaceID)
}
d.Set("output_json", outputJSON)
d.SetId(fmt.Sprintf("%s/%s", workspaceID, templateID))
d.Set("output_values", Flatten(items))
controller, err := getBaseController(meta)
if err != nil {
return err
}
d.Set(ResourceControllerURL, controller+"/schematics")
return nil
}