/
data_source_ibm_app.go
137 lines (130 loc) · 4.09 KB
/
data_source_ibm_app.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package cloudfoundry
import (
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMApp() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMAppRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name for the app",
},
"space_guid": {
Description: "Define space guid to which app belongs",
Type: schema.TypeString,
Required: true,
},
"memory": {
Description: "The amount of memory each instance should have. In megabytes.",
Type: schema.TypeInt,
Computed: true,
},
"instances": {
Description: "The number of instances",
Type: schema.TypeInt,
Computed: true,
},
"disk_quota": {
Description: "The maximum amount of disk available to an instance of an app. In megabytes.",
Type: schema.TypeInt,
Computed: true,
},
"buildpack": {
Description: "Buildpack to build the app. 3 options: a) Blank means autodetection; b) A Git Url pointing to a buildpack; c) Name of an installed buildpack.",
Type: schema.TypeString,
Computed: true,
},
"environment_json": {
Description: "Key/value pairs of all the environment variables to run in your app. Does not include any system or service variables.",
Type: schema.TypeMap,
Computed: true,
},
"route_guid": {
Description: "Define the route guids which should be bound to the application.",
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Computed: true,
},
"service_instance_guid": {
Description: "Define the service instance guids that should be bound to this application.",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"package_state": {
Description: "The state of the application package whether staged, pending etc",
Type: schema.TypeString,
Computed: true,
},
"state": {
Description: "The state of the application",
Type: schema.TypeString,
Computed: true,
},
"health_check_http_endpoint": {
Description: "Endpoint called to determine if the app is healthy.",
Type: schema.TypeString,
Computed: true,
},
"health_check_type": {
Description: "Type of health check to perform.",
Type: schema.TypeString,
Computed: true,
},
"health_check_timeout": {
Description: "Timeout in seconds for health checking of an staged app when starting up.",
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func dataSourceIBMAppRead(d *schema.ResourceData, meta interface{}) error {
cfClient, err := meta.(conns.ClientSession).MccpAPI()
if err != nil {
return err
}
appAPI := cfClient.Apps()
name := d.Get("name").(string)
spaceGUID := d.Get("space_guid").(string)
app, err := appAPI.FindByName(spaceGUID, name)
if err != nil {
return err
}
d.SetId(app.GUID)
d.Set("memory", app.Memory)
d.Set("disk_quota", app.DiskQuota)
if app.BuildPack != nil {
d.Set("buildpack", app.BuildPack)
}
d.Set("environment_json", flex.Flatten(app.EnvironmentJSON))
d.Set("package_state", app.PackageState)
d.Set("state", app.State)
d.Set("instances", app.Instances)
d.Set("health_check_type", app.HealthCheckType)
d.Set("health_check_http_endpoint", app.HealthCheckHTTPEndpoint)
d.Set("health_check_timeout", app.HealthCheckTimeout)
route, err := appAPI.ListRoutes(app.GUID)
if err != nil {
return err
}
if len(route) > 0 {
d.Set("route_guid", flex.FlattenRoute(route))
}
svcBindings, err := appAPI.ListServiceBindings(app.GUID)
if err != nil {
return err
}
if len(svcBindings) > 0 {
d.Set("service_instance_guid", flex.FlattenServiceBindings(svcBindings))
}
return nil
}