@@ -5,11 +5,12 @@ import (
55 "io"
66 "os"
77
8+ "github.com/ibm-verify/verify-sdk-go/pkg/config/security"
89 "github.com/ibm-verify/verifyctl/pkg/cmd/resource"
910 "github.com/ibm-verify/verifyctl/pkg/config"
10- "github.com/ibm-verify/verifyctl/pkg/module/security"
1111 cmdutil "github.com/ibm-verify/verifyctl/pkg/util/cmd"
1212 "github.com/ibm-verify/verifyctl/pkg/util/templates"
13+ "gopkg.in/yaml.v3"
1314
1415 contextx "github.com/ibm-verify/verify-sdk-go/pkg/core/context"
1516 errorsx "github.com/ibm-verify/verify-sdk-go/pkg/core/errors"
4344 apiClientExamples = templates .Examples (cmdutil .TranslateExamples (apiClientMessagePrefix , `
4445 # Create an empty API client resource.
4546 verifyctl create apiclient --boilerplate
47+
48+
49+ # Create an API client using a YAML file.
50+ verifyctl create -f=./apiclient.yaml
4651
4752 # Create an API client using a JSON file.
48- verifyctl create apiclient -f=./apiclient.json` ))
53+ verifyctl create -f=./apiclient.json` ))
4954)
5055
5156type apiClientOptions struct {
@@ -105,27 +110,26 @@ func (o *apiClientOptions) Run(cmd *cobra.Command, args []string) error {
105110 cmdutil .WriteString (cmd , entitlementsMessage + " " + apiClientEntitlements )
106111 return nil
107112 }
108-
109113 if o .boilerplate {
110114 resourceObj := & resource.ResourceObject {
111115 Kind : resource .ResourceTypePrefix + "ApiClient" ,
112116 APIVersion : "1.0" ,
113- Data : & security.Client {} ,
117+ Data : security .APIClientExample () ,
114118 }
115119
116120 cmdutil .WriteAsYAML (cmd , resourceObj , cmd .OutOrStdout ())
117121 return nil
118122 }
119123
120- auth , err := o .config .GetCurrentAuth ()
124+ _ , err := o .config .GetCurrentAuth ()
121125 if err != nil {
122126 return err
123127 }
124128
125- return o .createAPIClient (cmd , auth )
129+ return o .createAPIClient (cmd )
126130}
127131
128- func (o * apiClientOptions ) createAPIClient (cmd * cobra.Command , auth * config. AuthConfig ) error {
132+ func (o * apiClientOptions ) createAPIClient (cmd * cobra.Command ) error {
129133 ctx := cmd .Context ()
130134 vc := contextx .GetVerifyContext (ctx )
131135
@@ -135,15 +139,15 @@ func (o *apiClientOptions) createAPIClient(cmd *cobra.Command, auth *config.Auth
135139 return err
136140 }
137141
138- return o .createAPIClientWithData (cmd , auth , b )
142+ return o .createAPIClientWithData (cmd , b )
139143}
140144
141- func (o * apiClientOptions ) createAPIClientWithData (cmd * cobra.Command , auth * config. AuthConfig , data []byte ) error {
145+ func (o * apiClientOptions ) createAPIClientWithData (cmd * cobra.Command , data []byte ) error {
142146 ctx := cmd .Context ()
143147 vc := contextx .GetVerifyContext (ctx )
144148
145149 apiclient := & security.APIClientConfig {}
146- if err := json .Unmarshal (data , & apiclient ); err != nil {
150+ if err := yaml .Unmarshal (data , & apiclient ); err != nil {
147151 vc .Logger .Errorf ("unable to unmarshal API client; err=%v" , err )
148152 return err
149153 }
@@ -156,7 +160,7 @@ func (o *apiClientOptions) createAPIClientWithData(cmd *cobra.Command, auth *con
156160 }
157161
158162 client := security .NewAPIClient ()
159- resourceURI , err := client .CreateAPIClient (ctx , auth , apiclient )
163+ resourceURI , err := client .CreateAPIClient (ctx , apiclient )
160164 if err != nil {
161165 vc .Logger .Errorf ("failed to create API client; err=%v" , err )
162166 return err
@@ -166,11 +170,10 @@ func (o *apiClientOptions) createAPIClientWithData(cmd *cobra.Command, auth *con
166170 return nil
167171}
168172
169- func (o * apiClientOptions ) createAPIClientFromDataMap (cmd * cobra.Command , auth * config. AuthConfig , data map [string ]interface {}) error {
173+ func (o * apiClientOptions ) createAPIClientFromDataMap (cmd * cobra.Command , data map [string ]interface {}) error {
170174 ctx := cmd .Context ()
171175 vc := contextx .GetVerifyContext (ctx )
172176
173- // Convert map data to JSON
174177 apiclient := & security.APIClientConfig {}
175178 b , err := json .Marshal (data )
176179 if err != nil {
@@ -183,23 +186,20 @@ func (o *apiClientOptions) createAPIClientFromDataMap(cmd *cobra.Command, auth *
183186 return err
184187 }
185188
186- // Validate required fields
187189 if apiclient .ClientName == "" {
188190 return errorsx .G11NError ("clientName is required" )
189191 }
190192 if len (apiclient .Entitlements ) == 0 {
191193 return errorsx .G11NError ("entitlements list is required" )
192194 }
193195
194- // Create API client
195196 client := security .NewAPIClient ()
196- resourceURI , err := client .CreateAPIClient (ctx , auth , apiclient )
197+ resourceURI , err := client .CreateAPIClient (ctx , apiclient )
197198 if err != nil {
198199 vc .Logger .Errorf ("failed to create API client; err=%v" , err )
199200 return err
200201 }
201202
202- // Directly return the created resource URI
203203 cmdutil .WriteString (cmd , "Resource created: " + resourceURI )
204204 return nil
205205}
0 commit comments