Skip to content

Commit

Permalink
gcp fixes, adapted for new plugin-go
Browse files Browse the repository at this point in the history
  • Loading branch information
23doors committed Jul 8, 2021
1 parent dbf233f commit 490b69f
Show file tree
Hide file tree
Showing 24 changed files with 77 additions and 54 deletions.
16 changes: 16 additions & 0 deletions .semverbot.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[git]

[git.config]
email = "[email protected]"
name = "semverbot"

[git.tags]
prefix = "v"

[semver]
mode = "auto"

[semver.detection]
patch = ["fix/", "[fix]", "fix:"]
minor = ["feature/", "[feature]", "feat:", "feat/"]
major = ["release/", "[release]", "release:"]
30 changes: 16 additions & 14 deletions actions/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"

"github.com/outblocks/cli-plugin-gcp/deploy"
"github.com/outblocks/cli-plugin-gcp/deploy/gcp"
"github.com/outblocks/cli-plugin-gcp/gcp"
"github.com/outblocks/cli-plugin-gcp/internal/config"
"github.com/outblocks/outblocks-plugin-go/log"
"github.com/outblocks/outblocks-plugin-go/registry"
Expand All @@ -22,13 +22,13 @@ type PlanAction struct {
staticApps []*deploy.StaticApp
loadBalancer *deploy.LoadBalancer

PluginMap types.PluginStateMap
AppStates map[string]*types.AppState
DependencyStates map[string]*types.DependencyState
verify, fullCheck bool
PluginMap types.PluginStateMap
AppStates map[string]*types.AppState
DependencyStates map[string]*types.DependencyState
verify, destroy, fullCheck bool
}

func NewPlan(pctx *config.PluginContext, logger log.Logger, state types.PluginStateMap, appStates map[string]*types.AppState, depStates map[string]*types.DependencyState, verify, fullCheck bool) (*PlanAction, error) {
func NewPlan(pctx *config.PluginContext, logger log.Logger, state types.PluginStateMap, appStates map[string]*types.AppState, depStates map[string]*types.DependencyState, verify, destroy, fullCheck bool) (*PlanAction, error) {
if state == nil {
state = make(types.PluginStateMap)
}
Expand Down Expand Up @@ -61,6 +61,7 @@ func NewPlan(pctx *config.PluginContext, logger log.Logger, state types.PluginSt
AppStates: appStates,
DependencyStates: depStates,
verify: verify,
destroy: destroy,
fullCheck: fullCheck,
}, nil
}
Expand Down Expand Up @@ -145,11 +146,15 @@ func (p *PlanAction) enableAPIs(ctx context.Context) error {
}
}

diff, err := p.apiRegistry.Diff(ctx)
diff, err := p.apiRegistry.Diff(ctx, false)
if err != nil {
return err
}

if len(diff) != 0 {
p.log.Infoln("Enabling required Project Service APIs...")
}

err = p.apiRegistry.Apply(ctx, p.pluginCtx, diff, nil)
if err != nil {
return err
Expand Down Expand Up @@ -202,7 +207,7 @@ func (p *PlanAction) planAll(ctx context.Context, appPlans []*types.AppPlan) err

func (p *PlanAction) diff(ctx context.Context) (appPlanActions []*types.AppPlanActions, pluginPlanActions []*types.PluginPlanActions, err error) {
// Process diffs.
diff, err := p.registry.Diff(ctx)
diff, err := p.registry.Diff(ctx, p.destroy)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -300,20 +305,17 @@ func (p *PlanAction) Apply(ctx context.Context, appPlans []*types.AppPlan, cb fu
return err
}

diff, err := p.registry.Diff(ctx)
diff, err := p.registry.Diff(ctx, p.destroy)
if err != nil {
return err
}

err = p.registry.Apply(ctx, p.pluginCtx, diff, cb)
if err != nil {
return err
}
saveErr := p.save()

err = p.save()
if err != nil {
return err
}

return nil
return saveErr
}
2 changes: 1 addition & 1 deletion deploy/app_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/mitchellh/mapstructure"
"github.com/outblocks/cli-plugin-gcp/deploy/gcp"
"github.com/outblocks/cli-plugin-gcp/gcp"
"github.com/outblocks/cli-plugin-gcp/internal/config"
"github.com/outblocks/outblocks-plugin-go/registry"
"github.com/outblocks/outblocks-plugin-go/registry/fields"
Expand Down
2 changes: 1 addition & 1 deletion deploy/load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"sort"
"strings"

"github.com/outblocks/cli-plugin-gcp/deploy/gcp"
"github.com/outblocks/cli-plugin-gcp/gcp"
"github.com/outblocks/cli-plugin-gcp/internal/config"
"github.com/outblocks/outblocks-plugin-go/registry"
"github.com/outblocks/outblocks-plugin-go/registry/fields"
Expand Down
4 changes: 2 additions & 2 deletions deploy/gcp/address.go → gcp/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ func (o *Address) Read(ctx context.Context, meta interface{}) error {

addr, err := cli.GlobalAddresses.Get(projectID, name).Do()
if ErrIs404(err) {
o.SetNew(true)
o.MarkAsNew()

return nil
} else if err != nil {
return err
}

o.SetNew(false)
o.MarkAsExisting()
o.ProjectID.SetCurrent(projectID)
o.Name.SetCurrent(name)
o.IP.SetCurrent(addr.Address)
Expand Down
4 changes: 2 additions & 2 deletions deploy/gcp/api_service.go → gcp/api_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func (o *APIService) Read(ctx context.Context, meta interface{}) error {
}

if res.State != "ENABLED" {
o.SetNew(true)
o.MarkAsNew()
}

o.SetNew(false)
o.MarkAsExisting()
o.Name.SetCurrent(name)

return nil
Expand Down
4 changes: 2 additions & 2 deletions deploy/gcp/backend_service.go → gcp/backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ func (o *BackendService) Read(ctx context.Context, meta interface{}) error {

svc, err := cli.BackendServices.Get(projectID, name).Do()
if ErrIs404(err) {
o.SetNew(true)
o.MarkAsNew()

return nil
} else if err != nil {
return err
}

o.SetNew(false)
o.MarkAsExisting()
o.ProjectID.SetCurrent(projectID)
o.Name.SetCurrent(name)

Expand Down
4 changes: 2 additions & 2 deletions deploy/gcp/bucket.go → gcp/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (o *Bucket) Read(ctx context.Context, meta interface{}) error {

attrs, err := cli.Bucket(o.Name.Any()).Attrs(ctx)
if err == storage.ErrBucketNotExist {
o.SetNew(true)
o.MarkAsNew()

return nil
}
Expand All @@ -48,7 +48,7 @@ func (o *Bucket) Read(ctx context.Context, meta interface{}) error {

isNew := o.IsNew()

o.SetNew(false)
o.MarkAsExisting()
o.Name.SetCurrent(attrs.Name)
o.Location.SetCurrent(strings.ToLower(attrs.Location))
o.Versioning.SetCurrent(attrs.VersioningEnabled)
Expand Down
6 changes: 3 additions & 3 deletions deploy/gcp/bucket_object.go → gcp/bucket_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (o *BucketObject) Read(ctx context.Context, meta interface{}) error {
return ret, nil
})
if err == storage.ErrBucketNotExist {
o.SetNew(true)
o.MarkAsNew()

return nil
}
Expand All @@ -70,12 +70,12 @@ func (o *BucketObject) Read(ctx context.Context, meta interface{}) error {

attrs, ok := files.(map[string]*storage.ObjectAttrs)[o.Name.Any()]
if !ok {
o.SetNew(true)
o.MarkAsNew()

return nil
}

o.SetNew(false)
o.MarkAsExisting()
o.BucketName.SetCurrent(attrs.Bucket)
o.Name.SetCurrent(attrs.Name)
o.Hash.SetCurrent(hex.EncodeToString(attrs.MD5))
Expand Down
4 changes: 2 additions & 2 deletions deploy/gcp/cloud_run.go → gcp/cloud_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (o *CloudRun) Read(ctx context.Context, meta interface{}) error {

svc, err := getRunService(cli, projectID, name)
if ErrIs404(err) {
o.SetNew(true)
o.MarkAsNew()

return nil
}
Expand All @@ -60,7 +60,7 @@ func (o *CloudRun) Read(ctx context.Context, meta interface{}) error {

isNew := o.IsNew()

o.SetNew(false)
o.MarkAsExisting()
o.ProjectID.SetCurrent(projectID)
o.Name.SetCurrent(name)
o.Region.SetCurrent(region)
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions deploy/gcp/forwarding_rule.go → gcp/forwarding_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ func (o *ForwardingRule) Read(ctx context.Context, meta interface{}) error {

rule, err := cli.GlobalForwardingRules.Get(projectID, name).Do()
if ErrIs404(err) {
o.SetNew(true)
o.MarkAsNew()

return nil
} else if err != nil {
return err
}

o.SetNew(false)
o.MarkAsExisting()
o.ProjectID.SetCurrent(projectID)
o.Name.SetCurrent(name)
o.IPAddress.SetCurrent(rule.IPAddress)
Expand Down
9 changes: 7 additions & 2 deletions deploy/gcp/image.go → gcp/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ func (o *Image) Read(ctx context.Context, meta interface{}) error {

_, err = gcrremote.Head(ref, gcrremote.WithAuth(auth), gcrremote.WithContext(ctx))
if ErrIs404(err) {
o.SetNew(true)
o.MarkAsNew()

return nil
} else if err != nil {
return fmt.Errorf("error fetching image status: %w", err)
}

o.SetNew(false)
o.MarkAsExisting()
o.Name.SetCurrent(name)
o.GCR.SetCurrent(gcr)
o.ProjectID.SetCurrent(projectID)
Expand All @@ -98,6 +98,11 @@ func (o *Image) Create(ctx context.Context, meta interface{}) error {
return err
}

_, err = cli.Ping(ctx)
if err != nil {
return fmt.Errorf("docker is required for GCR image upload!\n%w", err)
}

reader, err := cli.ImagePull(ctx, GCSProxyDockerImage, dockertypes.ImagePullOptions{})
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions deploy/gcp/managed_ssl.go → gcp/managed_ssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ func (o *ManagedSSL) Read(ctx context.Context, meta interface{}) error {

cert, err := cli.SslCertificates.Get(projectID, name).Do()
if ErrIs404(err) {
o.SetNew(true)
o.MarkAsNew()

return nil
} else if err != nil {
return err
}

o.SetNew(false)
o.MarkAsExisting()
o.ProjectID.SetCurrent(projectID)
o.Name.SetCurrent(name)

Expand Down
4 changes: 2 additions & 2 deletions deploy/gcp/serverless_neg.go → gcp/serverless_neg.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ func (o *ServerlessNEG) Read(ctx context.Context, meta interface{}) error {

neg, err := cli.RegionNetworkEndpointGroups.Get(projectID, region, name).Do()
if ErrIs404(err) {
o.SetNew(true)
o.MarkAsNew()

return nil
} else if err != nil {
return err
}

o.SetNew(false)
o.MarkAsExisting()
o.ProjectID.SetCurrent(projectID)
o.Region.SetCurrent(region)
o.Name.SetCurrent(name)
Expand Down
4 changes: 2 additions & 2 deletions deploy/gcp/target_http_proxy.go → gcp/target_http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ func (o *TargetHTTPProxy) Read(ctx context.Context, meta interface{}) error {

proxy, err := cli.TargetHttpProxies.Get(projectID, name).Do()
if ErrIs404(err) {
o.SetNew(true)
o.MarkAsNew()

return nil
} else if err != nil {
return err
}

o.SetNew(false)
o.MarkAsExisting()
o.ProjectID.SetCurrent(projectID)
o.Name.SetCurrent(name)
o.URLMap.SetCurrent(proxy.UrlMap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ func (o *TargetHTTPSProxy) Read(ctx context.Context, meta interface{}) error {

proxy, err := cli.TargetHttpsProxies.Get(projectID, name).Do()
if ErrIs404(err) {
o.SetNew(true)
o.MarkAsNew()

return nil
} else if err != nil {
return err
}

o.SetNew(false)
o.MarkAsExisting()
o.ProjectID.SetCurrent(projectID)
o.Name.SetCurrent(name)
o.URLMap.SetCurrent(proxy.UrlMap)
Expand Down
4 changes: 2 additions & 2 deletions deploy/gcp/url_map.go → gcp/url_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ func (o *URLMap) Read(ctx context.Context, meta interface{}) error {

obj, err := cli.UrlMaps.Get(projectID, name).Do()
if ErrIs404(err) {
o.SetNew(true)
o.MarkAsNew()

return nil
} else if err != nil {
return err
}

o.SetNew(false)
o.MarkAsExisting()
o.ProjectID.SetCurrent(projectID)
o.Name.SetCurrent(name)

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-containerregistry v0.5.1
github.com/mitchellh/mapstructure v1.4.1
github.com/outblocks/outblocks-plugin-go v0.0.0-20210705152117-95220fb1419c
github.com/outblocks/outblocks-plugin-go v0.0.0-20210708161832-56a8cbbf879e
github.com/sirupsen/logrus v1.8.1 // indirect
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ github.com/outblocks/outblocks-plugin-go v0.0.0-20210630200127-298dfde78363 h1:V
github.com/outblocks/outblocks-plugin-go v0.0.0-20210630200127-298dfde78363/go.mod h1:vAn4Vv7fXTyrjNEvAVcKtKJ2Bwaqk3Oy63lqnBRIct4=
github.com/outblocks/outblocks-plugin-go v0.0.0-20210705152117-95220fb1419c h1:cyHK1LsYbaCS0lNG2J+fjZu2Ja8hu6Qod20Q9nrj4UQ=
github.com/outblocks/outblocks-plugin-go v0.0.0-20210705152117-95220fb1419c/go.mod h1:vAn4Vv7fXTyrjNEvAVcKtKJ2Bwaqk3Oy63lqnBRIct4=
github.com/outblocks/outblocks-plugin-go v0.0.0-20210708161832-56a8cbbf879e h1:vil3jRJL56zMzabUeFwmtI3KIm2TiHu0jswyAlxM1Mo=
github.com/outblocks/outblocks-plugin-go v0.0.0-20210708161832-56a8cbbf879e/go.mod h1:vAn4Vv7fXTyrjNEvAVcKtKJ2Bwaqk3Oy63lqnBRIct4=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
Expand Down
Loading

0 comments on commit 490b69f

Please sign in to comment.