/
data_source_ibm_schematics_state.go
77 lines (64 loc) · 1.77 KB
/
data_source_ibm_schematics_state.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
package ibm
import (
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceSchematicsState() *schema.Resource {
return &schema.Resource{
Read: resourceIBMSchematicsStateRead,
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",
},
"state_store": {
Type: schema.TypeString,
Computed: true,
},
"state_store_json": {
Type: schema.TypeString,
Computed: true,
},
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 resourceIBMSchematicsStateRead(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)
stateStore, err := wrkAPI.GetStateStore(workspaceID, templateID)
if err != nil {
return fmt.Errorf("Error retreiving statestore: %s", err)
}
statestr := fmt.Sprintf("%v", stateStore)
d.SetId(fmt.Sprintf("%s/%s", workspaceID, templateID))
d.Set("state_store", statestr)
stateByte, err := json.MarshalIndent(stateStore, "", "")
if err != nil {
return err
}
stateStoreJson := string(stateByte[:])
d.Set("state_store_json", stateStoreJson)
controller, err := getBaseController(meta)
if err != nil {
return err
}
d.Set(ResourceControllerURL, controller+"/schematics")
return nil
}