-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAPI_client.go
More file actions
211 lines (169 loc) · 5.82 KB
/
Copy pathAPI_client.go
File metadata and controls
211 lines (169 loc) · 5.82 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package create
import (
"encoding/json"
"io"
"os"
"github.ibm.com/sec-ci/devops-experiments/pkg/cmd/resource"
"github.ibm.com/sec-ci/devops-experiments/pkg/config"
"github.ibm.com/sec-ci/devops-experiments/pkg/module"
"github.ibm.com/sec-ci/devops-experiments/pkg/module/directory"
cmdutil "github.ibm.com/sec-ci/devops-experiments/pkg/util/cmd"
"github.ibm.com/sec-ci/devops-experiments/pkg/util/templates"
"gopkg.in/yaml.v3"
"github.com/spf13/cobra"
)
const (
apiClientUsage = "apiclient [options]"
apiClientMessagePrefix = "CreateApiClient"
apiClientEntitlements = "Manage API Clients"
apiClientResourceName = "apiclient"
)
var (
apiClientShortDesc = cmdutil.TranslateShortDesc(apiClientMessagePrefix, "Options to create an API client.")
apiClientLongDesc = templates.LongDesc(cmdutil.TranslateLongDesc(apiClientMessagePrefix, `
Options to create an API client.
API clients on Verify require specific entitlements, so ensure that the application or API client used
with the 'auth' command has the required entitlements.
An empty resource file can be generated using:
verifyctl create apiclient --boilerplate
You can check required entitlements by running:
verifyctl create apiclient --entitlements`))
apiClientExamples = templates.Examples(cmdutil.TranslateExamples(apiClientMessagePrefix, `
# Create an empty API client resource.
verifyctl create apiclient --boilerplate
# Create an API client using a JSON file.
verifyctl create apiclient -f=./apiclient.json`))
)
type apiClientOptions struct {
options
config *config.CLIConfig
}
func newApiClientCommand(config *config.CLIConfig, streams io.ReadWriter) *cobra.Command {
o := &apiClientOptions{
config: config,
}
cmd := &cobra.Command{
Use: apiClientUsage,
Short: apiClientShortDesc,
Long: apiClientLongDesc,
Example: apiClientExamples,
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.ExitOnError(cmd, o.Complete(cmd, args))
cmdutil.ExitOnError(cmd, o.Validate(cmd, args))
cmdutil.ExitOnError(cmd, o.Run(cmd, args))
},
}
cmd.SetOut(streams)
cmd.SetErr(streams)
cmd.SetIn(streams)
o.AddFlags(cmd)
return cmd
}
func (o *apiClientOptions) AddFlags(cmd *cobra.Command) {
o.addCommonFlags(cmd, apiClientResourceName)
cmd.Flags().StringVarP(&o.file, "file", "f", "", "Path to the yaml file containing API client data.")
}
func (o *apiClientOptions) Complete(cmd *cobra.Command, args []string) error {
return nil
}
func (o *apiClientOptions) Validate(cmd *cobra.Command, args []string) error {
if o.entitlements || o.boilerplate {
return nil
}
if len(o.file) == 0 {
return module.MakeSimpleError("The 'file' option is required if no other options are used.")
}
return nil
}
func (o *apiClientOptions) Run(cmd *cobra.Command, args []string) error {
if o.entitlements {
cmdutil.WriteString(cmd, entitlementsMessage+" "+apiClientEntitlements)
return nil
}
if o.boilerplate {
resourceObj := &resource.ResourceObject{
Kind: resource.ResourceTypePrefix + "ApiClient",
APIVersion: "1.0",
Data: &directory.Client{},
}
cmdutil.WriteAsYAML(cmd, resourceObj, cmd.OutOrStdout())
return nil
}
auth, err := o.config.GetCurrentAuth()
if err != nil {
return err
}
return o.createApiClient(cmd, auth)
}
func (o *apiClientOptions) createApiClient(cmd *cobra.Command, auth *config.AuthConfig) error {
ctx := cmd.Context()
vc := config.GetVerifyContext(ctx)
// Read the contents of the file
b, err := os.ReadFile(o.file)
if err != nil {
vc.Logger.Errorf("unable to read file; filename=%s, err=%v", o.file, err)
return err
}
// Create API client with data
return o.createApiClientWithData(cmd, auth, b)
}
func (o *apiClientOptions) createApiClientWithData(cmd *cobra.Command, auth *config.AuthConfig, data []byte) error {
ctx := cmd.Context()
vc := config.GetVerifyContext(ctx)
// Unmarshal yaml into API client struct
apiclient := &directory.Client{}
if err := yaml.Unmarshal(data, &apiclient); err != nil {
vc.Logger.Errorf("unable to unmarshal API client; err=%v", err)
return err
}
// Validate required fields
if apiclient.ClientName == "" {
return module.MakeSimpleError("clientName is required")
}
if len(apiclient.Entitlements) == 0 {
return module.MakeSimpleError("entitlements list is required")
}
// Create API client
client := directory.NewApiClient()
resourceURI, err := client.CreateApiClient(ctx, auth, apiclient)
if err != nil {
vc.Logger.Errorf("failed to create API client; err=%v", err)
return err
}
// Directly return the created resource URI
cmdutil.WriteString(cmd, "Resource created: "+resourceURI)
return nil
}
func (o *apiClientOptions) createApiClientFromDataMap(cmd *cobra.Command, auth *config.AuthConfig, data map[string]interface{}) error {
ctx := cmd.Context()
vc := config.GetVerifyContext(ctx)
// Convert map data to JSON
apiclient := &directory.Client{}
b, err := json.Marshal(data)
if err != nil {
vc.Logger.Errorf("failed to marshal data; err=%v", err)
return err
}
if err := json.Unmarshal(b, apiclient); err != nil {
vc.Logger.Errorf("unable to unmarshal data to API client; err=%v", err)
return err
}
// Validate required fields
if apiclient.ClientName == "" {
return module.MakeSimpleError("clientName is required")
}
if len(apiclient.Entitlements) == 0 {
return module.MakeSimpleError("entitlements list is required")
}
// Create API client
client := directory.NewApiClient()
resourceURI, err := client.CreateApiClient(ctx, auth, apiclient)
if err != nil {
vc.Logger.Errorf("failed to create API client; err=%v", err)
return err
}
// Directly return the created resource URI
cmdutil.WriteString(cmd, "Resource created: "+resourceURI)
return nil
}