/
data_source_ibm_app_route.go
92 lines (78 loc) · 2.22 KB
/
data_source_ibm_app_route.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package cloudfoundry
import (
"fmt"
v2 "github.com/IBM-Cloud/bluemix-go/api/mccp/mccpv2"
"github.com/IBM-Cloud/bluemix-go/helpers"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceIBMAppRoute() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMAppRouteRead,
Schema: map[string]*schema.Schema{
"space_guid": {
Description: "The guid of the space",
Type: schema.TypeString,
Required: true,
},
"domain_guid": {
Description: "The guid of the domain",
Type: schema.TypeString,
Required: true,
},
"host": {
Description: "The host of the route",
Type: schema.TypeString,
Optional: true,
},
"path": {
Type: schema.TypeString,
Optional: true,
Description: "The path of the route",
ValidateFunc: validate.ValidateRoutePath,
},
"port": {
Type: schema.TypeString,
Optional: true,
Description: "The port of the route",
ValidateFunc: validate.ValidateRoutePort,
},
},
}
}
func dataSourceIBMAppRouteRead(d *schema.ResourceData, meta interface{}) error {
cfClient, err := meta.(conns.ClientSession).MccpAPI()
if err != nil {
return err
}
spaceAPI := cfClient.Spaces()
spaceGUID := d.Get("space_guid").(string)
domainGUID := d.Get("domain_guid").(string)
params := v2.RouteFilter{
DomainGUID: domainGUID,
}
if host, ok := d.GetOk("host"); ok {
params.Host = helpers.String(host.(string))
}
if port, ok := d.GetOk("port"); ok {
params.Port = helpers.Int(port.(int))
}
if path, ok := d.GetOk("path"); ok {
params.Path = helpers.String(path.(string))
}
route, err := spaceAPI.ListRoutes(spaceGUID, params)
if err != nil {
return fmt.Errorf("[ERROR] Error retrieving route: %s", err)
}
if len(route) == 0 {
return fmt.Errorf("[ERROR] No route satifies the given parameters")
}
if len(route) > 1 {
return fmt.Errorf("[ERROR] More than one route satifies the given parameters")
}
d.SetId(route[0].GUID)
return nil
}