/
data_source_ibm_function_namespace.go
91 lines (77 loc) · 2.16 KB
/
data_source_ibm_function_namespace.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
package ibm
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.ibm.com/ibmcloud/namespace-go-sdk/ibmcloudfunctionsnamespaceapiv1"
)
func dataSourceIBMFunctionNamespace() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMFunctionNamespaceRead,
Schema: map[string]*schema.Schema{
funcNamespaceName: {
Type: schema.TypeString,
Required: true,
Description: "Name of namespace.",
ValidateFunc: InvokeValidator("ibm_function_namespace", funcNamespaceName),
},
funcNamespaceDesc: {
Type: schema.TypeString,
Computed: true,
Description: "Namespace Description.",
},
funcNamespaceResGrpId: {
Type: schema.TypeString,
Computed: true,
Description: "Resource Group ID.",
},
funcNamespaceLoc: {
Type: schema.TypeString,
Computed: true,
Description: "Namespace Location.",
},
},
}
}
func dataSourceIBMFunctionNamespaceRead(d *schema.ResourceData, meta interface{}) error {
nsClient, err := meta.(ClientSession).IAMNamespaceAPI()
if err != nil {
return err
}
namespaceOptions := &ibmcloudfunctionsnamespaceapiv1.GetNamespacesOptions{}
nsList, _, err := nsClient.GetNamespaces(namespaceOptions)
if err != nil {
return nil
}
name := d.Get("name").(string)
for _, n := range nsList.Namespaces {
if n.Name != nil && *n.Name == name {
getOptions := &ibmcloudfunctionsnamespaceapiv1.GetNamespaceOptions{
ID: n.ID,
}
instance, response, err := nsClient.GetNamespace(getOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
}
if instance.ID != nil {
d.SetId(*instance.ID)
}
if instance.Name != nil {
d.Set(funcNamespaceName, *instance.Name)
}
if instance.ResourceGroupID != nil {
d.Set(funcNamespaceResGrpId, *instance.ResourceGroupID)
}
if instance.Location != nil {
d.Set(funcNamespaceLoc, *instance.Location)
}
if instance.Description != nil {
d.Set(funcNamespaceDesc, *instance.Description)
}
return nil
}
}
return fmt.Errorf("No cloud function namespace found with name [%s]", name)
}