From 40cf816fe4bb792181b17eaae108e36c2b52b83c Mon Sep 17 00:00:00 2001 From: sleungcy-sap Date: Tue, 22 Oct 2024 11:09:40 -0700 Subject: [PATCH 1/3] adding go-cfclient --- cloudfoundry/managers/session.go | 23 ++++++++++++- .../managers/v3appdeployers/runbinder.go | 34 ++++++++++++------- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/cloudfoundry/managers/session.go b/cloudfoundry/managers/session.go index fd4e1c222..dba471e5f 100644 --- a/cloudfoundry/managers/session.go +++ b/cloudfoundry/managers/session.go @@ -27,12 +27,16 @@ import ( "github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers/noaa" "github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers/raw" "github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers/v3appdeployers" + + goClient "github.com/cloudfoundry/go-cfclient/v3/client" + goConfig "github.com/cloudfoundry/go-cfclient/v3/config" ) // Session - wraps the available clients from CF cli type Session struct { ClientV2 *ccv2.Client ClientV3 *ccv3.Client + ClientGo *goClient.Client ClientUAA *uaa.Client // Used for direct endpoint calls @@ -221,17 +225,20 @@ func (s *Session) init(config *configv3.Config, configUaa *configv3.Config, conf var accessToken string var refreshToken string var errType string + var goClientConfigOptions goConfig.Option tokFromStore := s.loadTokFromStoreIfNeed(configSess.StoreTokensPath, uaaClient.RefreshAccessToken) if tokFromStore.IsSet() { accessToken = tokFromStore.AccessToken refreshToken = tokFromStore.RefreshToken + goClientConfigOptions = goConfig.Token(accessToken, refreshToken) } else if configSess.SSOPasscode != "" { // try connecting with SSO passcode to retrieve access token and refresh token accessToken, refreshToken, err = uaaClient.Authenticate(map[string]string{ "passcode": configSess.SSOPasscode, }, configSess.Origin, constant.GrantTypePassword) errType = "SSO passcode" + goClientConfigOptions = goConfig.Token(accessToken, refreshToken) } else if config.CFUsername() != "" { // try connecting with pair given on uaa to retrieve access token and refresh token accessToken, refreshToken, err = uaaClient.Authenticate(map[string]string{ @@ -239,12 +246,14 @@ func (s *Session) init(config *configv3.Config, configUaa *configv3.Config, conf "password": config.CFPassword(), }, configSess.Origin, constant.GrantTypePassword) errType = "username/password" + goClientConfigOptions = goConfig.UserPassword(config.CFUsername(), config.CFPassword()) } else if config.UAAOAuthClient() != "cf" { accessToken, refreshToken, err = uaaClient.Authenticate(map[string]string{ "client_id": config.UAAOAuthClient(), "client_secret": config.UAAOAuthClientSecret(), }, configSess.Origin, constant.GrantTypeClientCredentials) errType = "client_id/client_secret" + goClientConfigOptions = goConfig.Token(accessToken, refreshToken) } if err != nil { 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 config.SetAccessToken(fmt.Sprintf("bearer %s", accessToken)) config.SetRefreshToken(refreshToken) + goconfig, err := goConfig.New(config.ConfigFile.Target, goClientConfigOptions) + + if err != nil { + return fmt.Errorf("Error when creating go-cfconfig: %s", err) + } + + goclient, err := goClient.New(goconfig) + if err != nil { + return fmt.Errorf("Error when creating go-cfclient: %s", err) + } + s.ClientGo = goclient + // Write access and refresh tokens to file if needed err = s.saveTokToStoreIfNeed(configSess.StoreTokensPath, accessToken, refreshToken) if err != nil { @@ -402,7 +423,7 @@ func (s *Session) loadDeployer() { s.Deployer = appdeployers.NewDeployer(stdStrategy, bgStrategy) // Initialize deployment strategies in v3 - s.V3RunBinder = v3appdeployers.NewRunBinder(s.ClientV3, s.NOAAClient) + s.V3RunBinder = v3appdeployers.NewRunBinder(s.ClientV3, s.ClientGo, s.NOAAClient) v3std := v3appdeployers.NewStandard(s.BitsManager, s.ClientV3, s.V3RunBinder) v3bg := v3appdeployers.NewBlueGreen(s.BitsManager, s.ClientV3, s.RawClient, s.V3RunBinder, v3std) diff --git a/cloudfoundry/managers/v3appdeployers/runbinder.go b/cloudfoundry/managers/v3appdeployers/runbinder.go index e54fc7a9a..bf9b7ca80 100644 --- a/cloudfoundry/managers/v3appdeployers/runbinder.go +++ b/cloudfoundry/managers/v3appdeployers/runbinder.go @@ -1,6 +1,7 @@ package v3appdeployers import ( + "context" "fmt" "time" @@ -8,6 +9,8 @@ import ( "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" "code.cloudfoundry.org/cli/resources" "code.cloudfoundry.org/cli/types" + goClient "github.com/cloudfoundry/go-cfclient/v3/client" + goResource "github.com/cloudfoundry/go-cfclient/v3/resource" "github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/common" "github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers/noaa" ) @@ -15,12 +18,14 @@ import ( type RunBinder struct { client *ccv3.Client noaaClient *noaa.NOAAClient + clientGo *goClient.Client } -func NewRunBinder(client *ccv3.Client, noaaClient *noaa.NOAAClient) *RunBinder { +func NewRunBinder(client *ccv3.Client, clientGo *goClient.Client, noaaClient *noaa.NOAAClient) *RunBinder { return &RunBinder{ client: client, noaaClient: noaaClient, + clientGo: clientGo, } } @@ -39,7 +44,14 @@ func (r RunBinder) MapRoutes(appDeploy AppDeploy) ([]resources.Route, error) { continue } - _, err = r.client.MapRoute(mappingCur.GUID, appGUID) + insertOrReplaceDestinations := goResource.RouteDestinationInsertOrReplace{ + App: goResource.RouteDestinationApp{ + GUID: &appGUID, + }, + Port: &mappingCur.Port, + } + _, err = r.clientGo.Routes.InsertDestinations(context.Background(), mappingCur.GUID, []*goResource.RouteDestinationInsertOrReplace{&insertOrReplaceDestinations}) + if err != nil { return mappings, err } @@ -48,18 +60,12 @@ func (r RunBinder) MapRoutes(appDeploy AppDeploy) ([]resources.Route, error) { // mostly due to route emitter to perform its action inside diego time.Sleep(1 * time.Second) - routeMappings, _, err := r.client.GetRouteDestinations(mappingCur.GUID) + mappingCreated, err = r.mappingExists(appGUID, mappingCur) + if err != nil { return mappings, err } - for _, mapping := range routeMappings { - if mapping.App.GUID == appGUID { - mappings = append(mappings, mappingCur) - mappingCreated = true - } - } - if !mappingCreated { return mappings, fmt.Errorf("Failed to map route %s", mappingCur.GUID) } @@ -69,16 +75,18 @@ func (r RunBinder) MapRoutes(appDeploy AppDeploy) ([]resources.Route, error) { } func (r RunBinder) mappingExists(appGUID string, curMapping resources.Route) (bool, error) { - mappings, _, err := r.client.GetRouteDestinations(curMapping.GUID) + destinations, err := r.clientGo.Routes.GetDestinations(context.Background(), curMapping.GUID) if err != nil { return false, err } - for _, mapping := range mappings { - if mapping.App.GUID == appGUID { + for _, destination := range destinations.Destinations { + if *destination.App.GUID == appGUID && + *destination.Port == curMapping.Port { return true, nil } + } return false, nil From c89a5a46f496351f4499b8ab41b6bb2ba47b006c Mon Sep 17 00:00:00 2001 From: sleungcy-sap Date: Tue, 22 Oct 2024 11:24:21 -0700 Subject: [PATCH 2/3] go mod --- go.mod | 11 ++++++++++- go.sum | 21 +++++++++++++++------ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index e9da2400f..d60db2fa5 100644 --- a/go.mod +++ b/go.mod @@ -43,6 +43,7 @@ require ( ) require ( + github.com/cloudfoundry/go-cfclient/v3 v3.0.0-alpha.9 github.com/google/uuid v1.3.1 github.com/hashicorp/terraform-plugin-log v0.9.0 ) @@ -55,8 +56,11 @@ require ( github.com/agext/levenshtein v1.2.2 // indirect github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect github.com/cloudflare/circl v1.3.7 // indirect + github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/emirpasic/gods v1.18.1 // indirect github.com/fatih/color v1.13.0 // indirect + github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab // indirect github.com/gobwas/glob v0.2.3 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/go-cmp v0.6.0 // indirect @@ -77,6 +81,7 @@ require ( github.com/hashicorp/yamux v0.1.1 // indirect github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect github.com/kevinburke/ssh_config v1.2.0 // indirect + github.com/martini-contrib/render v0.0.0-20150707142108-ec18f8345a11 // indirect github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-isatty v0.0.14 // indirect github.com/mattn/go-runewidth v0.0.4 // indirect @@ -87,9 +92,11 @@ require ( github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect github.com/oklog/run v1.0.0 // indirect + github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/sergi/go-diff v1.2.0 // indirect github.com/src-d/gcfg v1.4.0 // indirect - github.com/stretchr/testify v1.8.1 // indirect + github.com/stretchr/testify v1.9.0 // indirect github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect @@ -98,6 +105,7 @@ require ( golang.org/x/crypto v0.21.0 // indirect golang.org/x/mod v0.14.0 // indirect golang.org/x/net v0.23.0 // indirect + golang.org/x/oauth2 v0.21.0 // indirect golang.org/x/sys v0.18.0 // indirect golang.org/x/term v0.18.0 // indirect golang.org/x/tools v0.13.0 // indirect @@ -110,4 +118,5 @@ require ( gopkg.in/src-d/go-git.v4 v4.13.1 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index ad2f33e03..09ebdf586 100644 --- a/go.sum +++ b/go.sum @@ -244,6 +244,8 @@ github.com/cloudfoundry/bosh-cli v5.5.0+incompatible h1:P+hlG8D9xXIi75yqTpFrNBHR github.com/cloudfoundry/bosh-cli v5.5.0+incompatible/go.mod h1:rzIB+e1sn7wQL/TJ54bl/FemPKRhXby5BIMS3tLuWFM= github.com/cloudfoundry/bosh-utils v0.0.0-20190518100210-9f9df32d41c3 h1:SbXvMoOwvn5r/uOx3ylQ5pDQxWPaV9dPW5kQ5VpfmUQ= github.com/cloudfoundry/bosh-utils v0.0.0-20190518100210-9f9df32d41c3/go.mod h1:JCrKwetZGjxbfq1U139TZuXDBfdGLtjOEAfxMWKV/QM= +github.com/cloudfoundry/go-cfclient/v3 v3.0.0-alpha.9 h1:HK3+nJEPgwlhc5H74aw/V4mVowqWaTKGjHONdVQQ2Vw= +github.com/cloudfoundry/go-cfclient/v3 v3.0.0-alpha.9/go.mod h1:eUjFfpsU3lRv388wKlXMmkQfsJ9pveUHZEia7AoBCPY= github.com/cloudfoundry/noaa v2.1.0+incompatible h1:hr6VnM5VlYRN3YD+NmAedQLW8686sUMknOSe0mFS2vo= github.com/cloudfoundry/noaa v2.1.0+incompatible/go.mod h1:5LmacnptvxzrTvMfL9+EJhgkUfIgcwI61BVSTh47ECo= github.com/cloudfoundry/sonde-go v0.0.0-20171206171820-b33733203bb4 h1:cWfya7mo/zbnwYVio6eWGsFJHqYw4/k/uhwIJ1eqRPI= @@ -257,6 +259,8 @@ github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWH github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 h1:sDMmm+q/3+BukdIpxwO365v/Rbspp2Nt5XntgQRXq8Q= +github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcjuz89kmFXt9morQgcfYZAYZ5n8WHjt81YYWIwtTM= github.com/cppforlife/go-patch v0.2.0 h1:Y14MnCQjDlbw7WXT4k+u6DPAA9XnygN4BfrSpI/19RU= github.com/cppforlife/go-patch v0.2.0/go.mod h1:67a7aIi94FHDZdoeGSJRRFDp66l9MhaAG1yGxpUoFD8= github.com/creack/pty v1.1.7 h1:6pwm8kMQKCmgUg0ZHTm5+/YvRK0s3THD/28+T6/kk4A= @@ -298,6 +302,8 @@ github.com/go-git/go-git/v5 v5.10.1 h1:tu8/D8i+TWxgKpzQ3Vc43e+kkhXqtsZCKI/egajKn github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= +github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab h1:xveKWz2iaueeTaUgdetzel+U7exyigDYBryyVfV/rZk= +github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab/go.mod h1:/P9AEU963A2AYjv4d1V5eVL1CQbEJq6aCNHDDjibzu8= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= @@ -477,6 +483,8 @@ github.com/lunixbochs/vtclean v1.0.0 h1:xu2sLAri4lGiovBDQKxl5mrXyESr3gUr5m5SM5+L github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI= github.com/mailru/easyjson v0.0.0-20190403194419-1ea4449da983 h1:wL11wNW7dhKIcRCHSm4sHKPWz0tt4mwBsVodG7+Xyqg= github.com/mailru/easyjson v0.0.0-20190403194419-1ea4449da983/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/martini-contrib/render v0.0.0-20150707142108-ec18f8345a11 h1:YFh+sjyJTMQSYjKwM4dFKhJPJC/wfo98tPUc17HdoYw= +github.com/martini-contrib/render v0.0.0-20150707142108-ec18f8345a11/go.mod h1:Ah2dBMoxZEqk118as2T4u4fjfXarE0pPnMJaArZQZsI= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= @@ -523,6 +531,8 @@ github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAl github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro= github.com/onsi/gomega v1.20.0 h1:8W0cWlwFkflGPLltQvLRB7ZVD5HuP6ng320w2IS245Q= github.com/onsi/gomega v1.20.0/go.mod h1:DtrZpjmvpn2mPm4YWQa0/ALMDj9v4YxLgojwPeREyVo= +github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw= +github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0= github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo= github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -548,19 +558,16 @@ github.com/src-d/gcfg v1.4.0 h1:xXbNR5AlLSA315x2UO+fTSSAXCDf+Ar38/6oyGbDKQ4= github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tedsuo/rata v1.0.0 h1:Sf9aZrYy6ElSTncjnGkyC2yuVvz5YJetBIUKJ4CmeKE= github.com/tedsuo/rata v1.0.0/go.mod h1:X47ELzhOoLbfFIY0Cql9P6yo3Cdwf2CMX3FVZxRzJPc= github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= @@ -732,6 +739,8 @@ golang.org/x/oauth2 v0.0.0-20220822191816-0ebed06d0094/go.mod h1:h4gKUeWbJ4rQPri golang.org/x/oauth2 v0.0.0-20220909003341-f21342109be1/go.mod h1:h4gKUeWbJ4rQPri7E0u6Gs4e9Ri2zaLxzw5DI5XGrYg= golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783/go.mod h1:h4gKUeWbJ4rQPri7E0u6Gs4e9Ri2zaLxzw5DI5XGrYg= golang.org/x/oauth2 v0.1.0/go.mod h1:G9FE4dLTsbXUu90h/Pf85g4w1D+SSAgR+q46nJZ8M4A= +golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs= +golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= From a0e1c43080fd33d58d633e60317af3f26e319f6a Mon Sep 17 00:00:00 2001 From: sleungcy-sap Date: Wed, 13 Nov 2024 16:32:38 -0800 Subject: [PATCH 3/3] separate out routeservice changes --- cloudfoundry/managers/session.go | 5 +--- .../managers/v3appdeployers/runbinder.go | 29 ++++++++----------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/cloudfoundry/managers/session.go b/cloudfoundry/managers/session.go index dba471e5f..185e891b5 100644 --- a/cloudfoundry/managers/session.go +++ b/cloudfoundry/managers/session.go @@ -231,14 +231,12 @@ func (s *Session) init(config *configv3.Config, configUaa *configv3.Config, conf if tokFromStore.IsSet() { accessToken = tokFromStore.AccessToken refreshToken = tokFromStore.RefreshToken - goClientConfigOptions = goConfig.Token(accessToken, refreshToken) } else if configSess.SSOPasscode != "" { // try connecting with SSO passcode to retrieve access token and refresh token accessToken, refreshToken, err = uaaClient.Authenticate(map[string]string{ "passcode": configSess.SSOPasscode, }, configSess.Origin, constant.GrantTypePassword) errType = "SSO passcode" - goClientConfigOptions = goConfig.Token(accessToken, refreshToken) } else if config.CFUsername() != "" { // try connecting with pair given on uaa to retrieve access token and refresh token accessToken, refreshToken, err = uaaClient.Authenticate(map[string]string{ @@ -246,14 +244,12 @@ func (s *Session) init(config *configv3.Config, configUaa *configv3.Config, conf "password": config.CFPassword(), }, configSess.Origin, constant.GrantTypePassword) errType = "username/password" - goClientConfigOptions = goConfig.UserPassword(config.CFUsername(), config.CFPassword()) } else if config.UAAOAuthClient() != "cf" { accessToken, refreshToken, err = uaaClient.Authenticate(map[string]string{ "client_id": config.UAAOAuthClient(), "client_secret": config.UAAOAuthClientSecret(), }, configSess.Origin, constant.GrantTypeClientCredentials) errType = "client_id/client_secret" - goClientConfigOptions = goConfig.Token(accessToken, refreshToken) } if err != nil { return fmt.Errorf("Error when authenticate on cf using %s: %s", errType, err) @@ -265,6 +261,7 @@ func (s *Session) init(config *configv3.Config, configUaa *configv3.Config, conf config.SetAccessToken(fmt.Sprintf("bearer %s", accessToken)) config.SetRefreshToken(refreshToken) + goClientConfigOptions = goConfig.Token(accessToken, refreshToken) goconfig, err := goConfig.New(config.ConfigFile.Target, goClientConfigOptions) if err != nil { diff --git a/cloudfoundry/managers/v3appdeployers/runbinder.go b/cloudfoundry/managers/v3appdeployers/runbinder.go index bf9b7ca80..1981de9a4 100644 --- a/cloudfoundry/managers/v3appdeployers/runbinder.go +++ b/cloudfoundry/managers/v3appdeployers/runbinder.go @@ -1,7 +1,6 @@ package v3appdeployers import ( - "context" "fmt" "time" @@ -10,7 +9,6 @@ import ( "code.cloudfoundry.org/cli/resources" "code.cloudfoundry.org/cli/types" goClient "github.com/cloudfoundry/go-cfclient/v3/client" - goResource "github.com/cloudfoundry/go-cfclient/v3/resource" "github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/common" "github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers/noaa" ) @@ -44,14 +42,7 @@ func (r RunBinder) MapRoutes(appDeploy AppDeploy) ([]resources.Route, error) { continue } - insertOrReplaceDestinations := goResource.RouteDestinationInsertOrReplace{ - App: goResource.RouteDestinationApp{ - GUID: &appGUID, - }, - Port: &mappingCur.Port, - } - _, err = r.clientGo.Routes.InsertDestinations(context.Background(), mappingCur.GUID, []*goResource.RouteDestinationInsertOrReplace{&insertOrReplaceDestinations}) - + _, err = r.client.MapRoute(mappingCur.GUID, appGUID) if err != nil { return mappings, err } @@ -60,12 +51,18 @@ func (r RunBinder) MapRoutes(appDeploy AppDeploy) ([]resources.Route, error) { // mostly due to route emitter to perform its action inside diego time.Sleep(1 * time.Second) - mappingCreated, err = r.mappingExists(appGUID, mappingCur) - + routeMappings, _, err := r.client.GetRouteDestinations(mappingCur.GUID) if err != nil { return mappings, err } + for _, mapping := range routeMappings { + if mapping.App.GUID == appGUID { + mappings = append(mappings, mappingCur) + mappingCreated = true + } + } + if !mappingCreated { return mappings, fmt.Errorf("Failed to map route %s", mappingCur.GUID) } @@ -75,18 +72,16 @@ func (r RunBinder) MapRoutes(appDeploy AppDeploy) ([]resources.Route, error) { } func (r RunBinder) mappingExists(appGUID string, curMapping resources.Route) (bool, error) { - destinations, err := r.clientGo.Routes.GetDestinations(context.Background(), curMapping.GUID) + mappings, _, err := r.client.GetRouteDestinations(curMapping.GUID) if err != nil { return false, err } - for _, destination := range destinations.Destinations { - if *destination.App.GUID == appGUID && - *destination.Port == curMapping.Port { + for _, mapping := range mappings { + if mapping.App.GUID == appGUID { return true, nil } - } return false, nil