-
Notifications
You must be signed in to change notification settings - Fork 651
/
data_source_ibm_function_action.go
190 lines (175 loc) · 5.38 KB
/
data_source_ibm_function_action.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package functions
import (
"fmt"
"log"
"strings"
"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 DataSourceIBMFunctionAction() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMFunctionActionRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of action.",
},
"namespace": {
Type: schema.TypeString,
Required: true,
Description: "Name of the namespace.",
},
"limits": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"timeout": {
Type: schema.TypeInt,
Computed: true,
Description: "The timeout LIMIT in milliseconds after which the action is terminated.",
},
"memory": {
Type: schema.TypeInt,
Computed: true,
Description: "The maximum memory LIMIT in MB for the action (default 256.",
},
"log_size": {
Type: schema.TypeInt,
Computed: true,
Description: "The maximum log size LIMIT in MB for the action.",
},
},
},
},
"exec": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"image": {
Type: schema.TypeString,
Computed: true,
Description: "Container image name when kind is 'blackbox'.",
},
"init": {
Type: schema.TypeString,
Computed: true,
Description: "Optional zipfile reference.",
},
"code": {
Type: schema.TypeString,
Computed: true,
Description: "The code to execute when kind is not 'blackbox'.",
},
"kind": {
Type: schema.TypeString,
Computed: true,
Description: "The type of action. Possible values:php:7.3, nodejs:8, swift:3, nodejs, blackbox, java, sequence, nodejs:10, python:3, python, python:2, swift, swift:4.2.",
},
"main": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the action entry point (function or fully-qualified method name when applicable)",
},
"components": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "The List of fully qualified action",
},
},
},
},
"publish": {
Type: schema.TypeBool,
Computed: true,
Description: "Action visibilty.",
},
"version": {
Type: schema.TypeString,
Computed: true,
Description: "Semantic version of the item.",
},
"annotations": {
Type: schema.TypeString,
Computed: true,
Description: "All annotations set on action by user and those set by the IBM Cloud Function backend/API.",
},
"parameters": {
Type: schema.TypeString,
Computed: true,
Description: "All paramters set on action by user and those set by the IBM Cloud Function backend/API.",
},
"action_id": {
Type: schema.TypeString,
Computed: true,
},
"target_endpoint_url": {
Type: schema.TypeString,
Computed: true,
Description: "Action target endpoint URL.",
},
},
}
}
func dataSourceIBMFunctionActionRead(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
}
actionService := wskClient.Actions
name := d.Get("name").(string)
action, _, err := actionService.Get(name, true)
if err != nil {
return fmt.Errorf("[ERROR] Error retrieving IBM Cloud Function Action %s : %s", name, err)
}
temp := strings.Split(action.Namespace, "/")
pkgName := ""
if len(temp) == 2 {
pkgName = temp[1]
d.SetId(fmt.Sprintf("%s/%s", pkgName, action.Name))
d.Set("name", fmt.Sprintf("%s/%s", pkgName, action.Name))
} else {
d.SetId(action.Name)
d.Set("name", action.Name)
}
d.Set("namespace", namespace)
d.Set("limits", flex.FlattenLimits(action.Limits))
d.Set("exec", flex.FlattenExec(action.Exec, d))
d.Set("publish", action.Publish)
d.Set("version", action.Version)
d.Set("action_id", action.Name)
annotations, err := flex.FlattenAnnotations(action.Annotations)
if err != nil {
log.Printf(
"An error occured during reading of action (%s) annotations : %s", d.Id(), err)
}
d.Set("annotations", annotations)
parameters, err := flex.FlattenParameters(action.Parameters)
if err != nil {
log.Printf(
"An error occured during reading of action (%s) parameters : %s", d.Id(), err)
}
d.Set("parameters", parameters)
targetURL, err := action.ActionURL(wskClient.Config.Host, "/api", wskClient.Config.Version, pkgName)
if err != nil {
log.Printf(
"An error occured during reading of action (%s) targetURL : %s", d.Id(), err)
}
d.Set("target_endpoint_url", targetURL)
return nil
}