Skip to content

Commit 40cf816

Browse files
committed
adding go-cfclient
1 parent ea11e63 commit 40cf816

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

cloudfoundry/managers/session.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ import (
2727
"github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers/noaa"
2828
"github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers/raw"
2929
"github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers/v3appdeployers"
30+
31+
goClient "github.com/cloudfoundry/go-cfclient/v3/client"
32+
goConfig "github.com/cloudfoundry/go-cfclient/v3/config"
3033
)
3134

3235
// Session - wraps the available clients from CF cli
3336
type Session struct {
3437
ClientV2 *ccv2.Client
3538
ClientV3 *ccv3.Client
39+
ClientGo *goClient.Client
3640
ClientUAA *uaa.Client
3741

3842
// Used for direct endpoint calls
@@ -221,30 +225,35 @@ func (s *Session) init(config *configv3.Config, configUaa *configv3.Config, conf
221225
var accessToken string
222226
var refreshToken string
223227
var errType string
228+
var goClientConfigOptions goConfig.Option
224229

225230
tokFromStore := s.loadTokFromStoreIfNeed(configSess.StoreTokensPath, uaaClient.RefreshAccessToken)
226231
if tokFromStore.IsSet() {
227232
accessToken = tokFromStore.AccessToken
228233
refreshToken = tokFromStore.RefreshToken
234+
goClientConfigOptions = goConfig.Token(accessToken, refreshToken)
229235
} else if configSess.SSOPasscode != "" {
230236
// try connecting with SSO passcode to retrieve access token and refresh token
231237
accessToken, refreshToken, err = uaaClient.Authenticate(map[string]string{
232238
"passcode": configSess.SSOPasscode,
233239
}, configSess.Origin, constant.GrantTypePassword)
234240
errType = "SSO passcode"
241+
goClientConfigOptions = goConfig.Token(accessToken, refreshToken)
235242
} else if config.CFUsername() != "" {
236243
// try connecting with pair given on uaa to retrieve access token and refresh token
237244
accessToken, refreshToken, err = uaaClient.Authenticate(map[string]string{
238245
"username": config.CFUsername(),
239246
"password": config.CFPassword(),
240247
}, configSess.Origin, constant.GrantTypePassword)
241248
errType = "username/password"
249+
goClientConfigOptions = goConfig.UserPassword(config.CFUsername(), config.CFPassword())
242250
} else if config.UAAOAuthClient() != "cf" {
243251
accessToken, refreshToken, err = uaaClient.Authenticate(map[string]string{
244252
"client_id": config.UAAOAuthClient(),
245253
"client_secret": config.UAAOAuthClientSecret(),
246254
}, configSess.Origin, constant.GrantTypeClientCredentials)
247255
errType = "client_id/client_secret"
256+
goClientConfigOptions = goConfig.Token(accessToken, refreshToken)
248257
}
249258
if err != nil {
250259
return fmt.Errorf("Error when authenticate on cf using %s: %s", errType, err)
@@ -256,6 +265,18 @@ func (s *Session) init(config *configv3.Config, configUaa *configv3.Config, conf
256265
config.SetAccessToken(fmt.Sprintf("bearer %s", accessToken))
257266
config.SetRefreshToken(refreshToken)
258267

268+
goconfig, err := goConfig.New(config.ConfigFile.Target, goClientConfigOptions)
269+
270+
if err != nil {
271+
return fmt.Errorf("Error when creating go-cfconfig: %s", err)
272+
}
273+
274+
goclient, err := goClient.New(goconfig)
275+
if err != nil {
276+
return fmt.Errorf("Error when creating go-cfclient: %s", err)
277+
}
278+
s.ClientGo = goclient
279+
259280
// Write access and refresh tokens to file if needed
260281
err = s.saveTokToStoreIfNeed(configSess.StoreTokensPath, accessToken, refreshToken)
261282
if err != nil {
@@ -402,7 +423,7 @@ func (s *Session) loadDeployer() {
402423
s.Deployer = appdeployers.NewDeployer(stdStrategy, bgStrategy)
403424

404425
// Initialize deployment strategies in v3
405-
s.V3RunBinder = v3appdeployers.NewRunBinder(s.ClientV3, s.NOAAClient)
426+
s.V3RunBinder = v3appdeployers.NewRunBinder(s.ClientV3, s.ClientGo, s.NOAAClient)
406427
v3std := v3appdeployers.NewStandard(s.BitsManager, s.ClientV3, s.V3RunBinder)
407428
v3bg := v3appdeployers.NewBlueGreen(s.BitsManager, s.ClientV3, s.RawClient, s.V3RunBinder, v3std)
408429

cloudfoundry/managers/v3appdeployers/runbinder.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
package v3appdeployers
22

33
import (
4+
"context"
45
"fmt"
56
"time"
67

78
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
89
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
910
"code.cloudfoundry.org/cli/resources"
1011
"code.cloudfoundry.org/cli/types"
12+
goClient "github.com/cloudfoundry/go-cfclient/v3/client"
13+
goResource "github.com/cloudfoundry/go-cfclient/v3/resource"
1114
"github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/common"
1215
"github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers/noaa"
1316
)
1417

1518
type RunBinder struct {
1619
client *ccv3.Client
1720
noaaClient *noaa.NOAAClient
21+
clientGo *goClient.Client
1822
}
1923

20-
func NewRunBinder(client *ccv3.Client, noaaClient *noaa.NOAAClient) *RunBinder {
24+
func NewRunBinder(client *ccv3.Client, clientGo *goClient.Client, noaaClient *noaa.NOAAClient) *RunBinder {
2125
return &RunBinder{
2226
client: client,
2327
noaaClient: noaaClient,
28+
clientGo: clientGo,
2429
}
2530
}
2631

@@ -39,7 +44,14 @@ func (r RunBinder) MapRoutes(appDeploy AppDeploy) ([]resources.Route, error) {
3944
continue
4045
}
4146

42-
_, err = r.client.MapRoute(mappingCur.GUID, appGUID)
47+
insertOrReplaceDestinations := goResource.RouteDestinationInsertOrReplace{
48+
App: goResource.RouteDestinationApp{
49+
GUID: &appGUID,
50+
},
51+
Port: &mappingCur.Port,
52+
}
53+
_, err = r.clientGo.Routes.InsertDestinations(context.Background(), mappingCur.GUID, []*goResource.RouteDestinationInsertOrReplace{&insertOrReplaceDestinations})
54+
4355
if err != nil {
4456
return mappings, err
4557
}
@@ -48,18 +60,12 @@ func (r RunBinder) MapRoutes(appDeploy AppDeploy) ([]resources.Route, error) {
4860
// mostly due to route emitter to perform its action inside diego
4961
time.Sleep(1 * time.Second)
5062

51-
routeMappings, _, err := r.client.GetRouteDestinations(mappingCur.GUID)
63+
mappingCreated, err = r.mappingExists(appGUID, mappingCur)
64+
5265
if err != nil {
5366
return mappings, err
5467
}
5568

56-
for _, mapping := range routeMappings {
57-
if mapping.App.GUID == appGUID {
58-
mappings = append(mappings, mappingCur)
59-
mappingCreated = true
60-
}
61-
}
62-
6369
if !mappingCreated {
6470
return mappings, fmt.Errorf("Failed to map route %s", mappingCur.GUID)
6571
}
@@ -69,16 +75,18 @@ func (r RunBinder) MapRoutes(appDeploy AppDeploy) ([]resources.Route, error) {
6975
}
7076

7177
func (r RunBinder) mappingExists(appGUID string, curMapping resources.Route) (bool, error) {
72-
mappings, _, err := r.client.GetRouteDestinations(curMapping.GUID)
78+
destinations, err := r.clientGo.Routes.GetDestinations(context.Background(), curMapping.GUID)
7379

7480
if err != nil {
7581
return false, err
7682
}
7783

78-
for _, mapping := range mappings {
79-
if mapping.App.GUID == appGUID {
84+
for _, destination := range destinations.Destinations {
85+
if *destination.App.GUID == appGUID &&
86+
*destination.Port == curMapping.Port {
8087
return true, nil
8188
}
89+
8290
}
8391

8492
return false, nil

0 commit comments

Comments
 (0)