-
Notifications
You must be signed in to change notification settings - Fork 650
/
data_source_ibm_function_namespace.go
93 lines (78 loc) · 2.26 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
92
93
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package functions
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM-Cloud/bluemix-go/api/functions"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
)
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: validate.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 {
functionNamespaceAPI, err := meta.(conns.ClientSession).FunctionIAMNamespaceAPI()
if err != nil {
return err
}
name := d.Get("name").(string)
nsList, err := functionNamespaceAPI.Namespaces().GetNamespaces()
if err != nil {
return err
}
for _, n := range nsList.Namespaces {
if n.Name != nil && *n.Name == name {
getOptions := functions.GetNamespaceOptions{
ID: n.ID,
}
instance, err := functionNamespaceAPI.Namespaces().GetNamespace(getOptions)
if err != nil {
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("[ERROR] No cloud function namespace found with name [%s]", name)
}