11package cfrestclient
22
33import (
4+ "bytes"
45 "crypto/md5"
56 "crypto/tls"
67 "encoding/hex"
@@ -60,7 +61,7 @@ func (c CloudFoundryRestClient) GetAppProcessStatistics(appGuid string) ([]model
6061 apiEndpoint , _ := c .cliConn .ApiEndpoint ()
6162
6263 getAppProcessStatsUrl := fmt .Sprintf ("%s/%sapps/%s/processes/web/stats" , apiEndpoint , cfBaseUrl , appGuid )
63- body , err := executeRequest (getAppProcessStatsUrl , token , c .isSslDisabled )
64+ body , err := executeRequest ("GET" , getAppProcessStatsUrl , token , c .isSslDisabled , nil )
6465 if err != nil {
6566 return nil , err
6667 }
@@ -135,10 +136,61 @@ func (c CloudFoundryRestClient) GetServiceInstanceByName(serviceName, spaceGuid
135136 return resultService , nil
136137}
137138
139+ func (c CloudFoundryRestClient ) CreateUserProvidedServiceInstance (serviceName string , spaceGuid string , credentials map [string ]string ) (models.CloudFoundryServiceInstance , error ) {
140+ token , err := c .cliConn .AccessToken ()
141+ if err != nil {
142+ return models.CloudFoundryServiceInstance {}, fmt .Errorf ("failed to retrieve access token: %s" , err )
143+ }
144+
145+ apiEndpoint , _ := c .cliConn .ApiEndpoint ()
146+
147+ createServiceURL := fmt .Sprintf ("%s/%sservice_instances" , apiEndpoint , cfBaseUrl )
148+
149+ payload := map [string ]any {
150+ "type" : "user-provided" ,
151+ "name" : serviceName ,
152+ "relationships" : map [string ]any {
153+ "space" : map [string ]any {
154+ "data" : map [string ]any {
155+ "guid" : spaceGuid ,
156+ },
157+ },
158+ },
159+ }
160+
161+ if credentials != nil {
162+ payload ["credentials" ] = credentials
163+ }
164+
165+ jsonBody , err := json .Marshal (payload )
166+ if err != nil {
167+ return models.CloudFoundryServiceInstance {}, fmt .Errorf ("failed to marshal create UPS request: %w" , err )
168+ }
169+
170+ body , err := executeRequest (http .MethodPost , createServiceURL , token , c .isSslDisabled , jsonBody )
171+ if err != nil {
172+ return models.CloudFoundryServiceInstance {}, err
173+ }
174+
175+ response , err := parseBody [models.CloudFoundryUserProvidedServiceInstance ](body )
176+ if err != nil {
177+ return models.CloudFoundryServiceInstance {}, err
178+ }
179+
180+ return models.CloudFoundryServiceInstance {
181+ Guid : response .Guid ,
182+ Name : response .Name ,
183+ Type : response .Type ,
184+ LastOperation : response .LastOperation ,
185+ SpaceGuid : response .SpaceGuid ,
186+ Metadata : response .Metadata ,
187+ }, nil
188+ }
189+
138190func getPaginatedResources [T any ](url , token string , isSslDisabled bool ) ([]T , error ) {
139191 var result []T
140192 for url != "" {
141- body , err := executeRequest (url , token , isSslDisabled )
193+ body , err := executeRequest ("GET" , url , token , isSslDisabled , nil )
142194 if err != nil {
143195 return nil , err
144196 }
@@ -158,7 +210,7 @@ func getPaginatedResources[T any](url, token string, isSslDisabled bool) ([]T, e
158210func getPaginatedResourcesWithIncluded [T any , Auxiliary any ](url , token string , isSslDisabled bool , auxiliaryContentHandler func (T , Auxiliary ) T ) ([]T , error ) {
159211 var result []T
160212 for url != "" {
161- body , err := executeRequest (url , token , isSslDisabled )
213+ body , err := executeRequest ("GET" , url , token , isSslDisabled , nil )
162214 if err != nil {
163215 return nil , err
164216 }
@@ -175,9 +227,22 @@ func getPaginatedResourcesWithIncluded[T any, Auxiliary any](url, token string,
175227 return result , nil
176228}
177229
178- func executeRequest (url , token string , isSslDisabled bool ) ([]byte , error ) {
179- req , _ := http .NewRequest (http .MethodGet , url , nil )
180- req .Header .Add ("Authorization" , token )
230+ func executeRequest (methodType , url , token string , isSslDisabled bool , body []byte ) ([]byte , error ) {
231+ var reader io.Reader
232+
233+ if body != nil {
234+ reader = bytes .NewReader (body )
235+ }
236+ request , err := http .NewRequest (methodType , url , reader )
237+ if err != nil {
238+ return nil , err
239+ }
240+
241+ request .Header .Add ("Authorization" , token )
242+ request .Header .Set ("Accept" , "application/json" )
243+ if body != nil {
244+ request .Header .Set ("Content-Type" , "application/json" )
245+ }
181246
182247 // Create transport with TLS configuration
183248 httpTransport := http .DefaultTransport .(* http.Transport ).Clone ()
@@ -187,7 +252,7 @@ func executeRequest(url, token string, isSslDisabled bool) ([]byte, error) {
187252 userAgentTransport := baseclient .NewUserAgentTransport (httpTransport )
188253
189254 client := & http.Client {Transport : userAgentTransport }
190- resp , err := client .Do (req )
255+ resp , err := client .Do (request )
191256 if err != nil {
192257 return nil , err
193258 }
0 commit comments