-
Notifications
You must be signed in to change notification settings - Fork 670
/
data_source_ibm_function_package.go
115 lines (99 loc) · 2.97 KB
/
data_source_ibm_function_package.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package functions
import (
"fmt"
"log"
"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 DataSourceIBMFunctionPackage() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMFunctionPackageRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the package.",
},
"namespace": {
Type: schema.TypeString,
Required: true,
Description: "Name of the namespace.",
},
"publish": {
Type: schema.TypeBool,
Computed: true,
Description: "Package Visibility.",
},
"version": {
Type: schema.TypeString,
Computed: true,
Description: "Semantic version of the package.",
},
"annotations": {
Type: schema.TypeString,
Computed: true,
Description: "All annotations set on package by user and those set by the IBM Cloud Function backend/API.",
},
"parameters": {
Type: schema.TypeString,
Computed: true,
Description: "All parameters set on package by user and those set by the IBM Cloud Function backend/API.",
},
"bind_package_name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of binded package.",
},
"package_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceIBMFunctionPackageRead(d *schema.ResourceData, meta interface{}) error {
functionNamespaceAPI, err := meta.(conns.ClientSession).FunctionIAMNamespaceAPI()
if err != nil {
return err
}
bxSession, err := meta.(conns.ClientSession).BluemixSession()
if err != nil {
return err
}
namespace := d.Get("namespace").(string)
wskClient, err := conns.SetupOpenWhiskClientConfig(namespace, bxSession, functionNamespaceAPI)
if err != nil {
return err
}
packageService := wskClient.Packages
name := d.Get("name").(string)
pkg, _, err := packageService.Get(name)
if err != nil {
return fmt.Errorf("[ERROR] Error retrieving IBM Cloud Function package %s : %s", name, err)
}
d.SetId(pkg.Name)
d.Set("name", pkg.Name)
d.Set("namespace", namespace)
d.Set("publish", pkg.Publish)
d.Set("version", pkg.Version)
d.Set("package_id", pkg.Name)
annotations, err := flex.FlattenAnnotations(pkg.Annotations)
if err != nil {
log.Printf(
"An error occured during reading of package (%s) annotations : %s", d.Id(), err)
}
d.Set("annotations", annotations)
parameters, err := flex.FlattenParameters(pkg.Parameters)
if err != nil {
log.Printf(
"An error occured during reading of package (%s) parameters : %s", d.Id(), err)
}
d.Set("parameters", parameters)
if !flex.IsEmpty(*pkg.Binding) {
d.Set("bind_package_name", fmt.Sprintf("/%s/%s", pkg.Binding.Namespace, pkg.Binding.Name))
}
return nil
}