Skip to content

Commit

Permalink
alpha: monitoring support
Browse files Browse the repository at this point in the history
  • Loading branch information
23doors committed Aug 5, 2022
1 parent 9877cd9 commit 7f3d24c
Show file tree
Hide file tree
Showing 18 changed files with 880 additions and 65 deletions.
6 changes: 3 additions & 3 deletions deploy/load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ func (o *LoadBalancer) processDomain(pctx *config.PluginContext, r *registry.Reg
}

if domainInfo != nil {
if domainInfo.Other.GetFields() == nil {
domainInfo.Other, _ = structpb.NewStruct(nil)
if domainInfo.Properties.GetFields() == nil {
domainInfo.Properties, _ = structpb.NewStruct(nil)
}

domainInfo.Other.GetFields()["cloudflare_proxy"] = structpb.NewBoolValue(false)
domainInfo.Properties.GetFields()["cloudflare_proxy"] = structpb.NewBoolValue(false)
}

o.ManagedSSLs = append(o.ManagedSSLs, cert)
Expand Down
8 changes: 4 additions & 4 deletions gcp/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (o *Bucket) Read(ctx context.Context, meta interface{}) error {
return fmt.Errorf("error fetching bucket policy: %w", err)
}

o.Public.SetCurrent(policy.HasRole("allUsers", "roles/storage.objectViewer"))
o.Public.SetCurrent(policy.HasRole(ACLAllUsers, "roles/storage.objectViewer"))

return nil
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func (o *Bucket) Create(ctx context.Context, meta interface{}) error {
return fmt.Errorf("error fetching bucket policy: %w", err)
}

policy.Add("allUsers", "roles/storage.objectViewer")
policy.Add(ACLAllUsers, "roles/storage.objectViewer")

err = b.IAM().SetPolicy(ctx, policy)
if err != nil {
Expand Down Expand Up @@ -252,9 +252,9 @@ func (o *Bucket) Update(ctx context.Context, meta interface{}) error {
}

if o.Public.Wanted() {
policy.Add("allUsers", "roles/storage.objectViewer")
policy.Add(ACLAllUsers, "roles/storage.objectViewer")
} else {
policy.Remove("allUsers", "roles/storage.objectViewer")
policy.Remove(ACLAllUsers, "roles/storage.objectViewer")
}

err = b.IAM().SetPolicy(ctx, policy)
Expand Down
14 changes: 7 additions & 7 deletions gcp/cloud_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (o *CloudFunction) Read(ctx context.Context, meta interface{}) error {

for _, b := range policy.Bindings {
if b.Role == "roles/cloudfunctions.invoker" {
isPublic = plugin_util.StringSliceContains(b.Members, "allUsers")
isPublic = plugin_util.StringSliceContains(b.Members, ACLAllUsers)
break
}
}
Expand Down Expand Up @@ -194,7 +194,7 @@ func (o *CloudFunction) Create(ctx context.Context, meta interface{}) error {
}

policy.Bindings = append(policy.Bindings, &cloudfunctions.Binding{
Members: []string{"allUsers"},
Members: []string{ACLAllUsers},
Role: "roles/cloudfunctions.invoker",
})

Expand Down Expand Up @@ -251,7 +251,7 @@ func (o *CloudFunction) Update(ctx context.Context, meta interface{}) error {
for _, b := range policy.Bindings {
if b.Role == "roles/cloudfunctions.invoker" {
if !o.IsPublic.Wanted() {
if !plugin_util.StringSliceContains(b.Members, "allUsers") {
if !plugin_util.StringSliceContains(b.Members, ACLAllUsers) {
newBindings = append(newBindings, b)
continue
}
Expand All @@ -264,14 +264,14 @@ func (o *CloudFunction) Update(ctx context.Context, meta interface{}) error {
var newMembers []string

for _, m := range b.Members {
if m != "allUsers" {
if m != ACLAllUsers {
newMembers = append(newMembers, m)
}
}

b.Members = newMembers
} else if !plugin_util.StringSliceContains(b.Members, "allUsers") {
b.Members = append(b.Members, "allUsers")
} else if !plugin_util.StringSliceContains(b.Members, ACLAllUsers) {
b.Members = append(b.Members, ACLAllUsers)
added = true
}
}
Expand All @@ -281,7 +281,7 @@ func (o *CloudFunction) Update(ctx context.Context, meta interface{}) error {

if o.IsPublic.Wanted() && !added {
newBindings = append(newBindings, &cloudfunctions.Binding{
Members: []string{"allUsers"},
Members: []string{ACLAllUsers},
Role: "roles/cloudfunctions.invoker",
})
}
Expand Down
4 changes: 2 additions & 2 deletions gcp/cloud_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (o *CloudRun) Read(ctx context.Context, meta interface{}) error { // nolint
return err
}

if err == nil && pol != nil && len(pol.Bindings) == 1 && len(pol.Bindings[0].Members) == 1 && pol.Bindings[0].Role == "roles/run.invoker" && pol.Bindings[0].Members[0] == "allUsers" {
if err == nil && pol != nil && len(pol.Bindings) == 1 && len(pol.Bindings[0].Members) == 1 && pol.Bindings[0].Role == "roles/run.invoker" && pol.Bindings[0].Members[0] == ACLAllUsers {
o.IsPublic.SetCurrent(true)
} else {
o.IsPublic.SetCurrent(false)
Expand Down Expand Up @@ -334,7 +334,7 @@ func setRunServiceIAMPolicy(cli *run.APIService, project, region, name string, p

if public {
policy = &run.Policy{Bindings: []*run.Binding{{
Members: []string{"allUsers"},
Members: []string{ACLAllUsers},
Role: "roles/run.invoker",
}}}
}
Expand Down
3 changes: 2 additions & 1 deletion gcp/consts.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package gcp

var (
APISRequired = []string{"run.googleapis.com", "containerregistry.googleapis.com", "compute.googleapis.com", "sqladmin.googleapis.com", "secretmanager.googleapis.com", "cloudresourcemanager.googleapis.com", "cloudfunctions.googleapis.com"}
APISRequired = []string{"run.googleapis.com", "containerregistry.googleapis.com", "compute.googleapis.com", "sqladmin.googleapis.com", "secretmanager.googleapis.com", "cloudresourcemanager.googleapis.com", "cloudfunctions.googleapis.com", "monitoring.googleapis.com"}
ValidRegions = []string{"asia-east1", "asia-east2", "asia-northeast1", "asia-northeast2", "asia-northeast3", "asia-south1", "asia-southeast1", "australia-southeast1", "europe-north1", "europe-west1", "europe-west2", "europe-west3", "europe-west4", "europe-west6", "northamerica-northeast1", "southamerica-east1", "us-central1", "us-east1", "us-east4", "us-west1", "us-west2", "us-west3"}
)

const (
ACLPublicRead = "publicRead"
ACLProjectPrivate = "projectPrivate"
ACLAllUsers = "allUsers"

OperationDone = "DONE"
CloudRunReady = "Ready"
Expand Down
148 changes: 148 additions & 0 deletions gcp/notification_channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package gcp

import (
"context"
"fmt"

"github.com/outblocks/cli-plugin-gcp/internal/config"
"github.com/outblocks/outblocks-plugin-go/registry"
"github.com/outblocks/outblocks-plugin-go/registry/fields"
"google.golang.org/genproto/googleapis/monitoring/v3"
)

type NotificationChannel struct {
registry.ResourceBase

ID fields.StringOutputField
DisplayName fields.StringInputField `default:"Outblocks Notification Channel"`
ProjectID fields.StringInputField `state:"force_new"`
Type fields.StringInputField
Labels fields.MapInputField
}

func (o *NotificationChannel) ReferenceID() string {
return o.ID.Current()
}

func (o *NotificationChannel) GetName() string {
return fields.VerboseString(o.DisplayName)
}

func (o *NotificationChannel) Read(ctx context.Context, meta interface{}) error {
pctx := meta.(*config.PluginContext)

cli, err := pctx.GCPMonitoringNotificationChannelClient(ctx)
if err != nil {
return err
}

projectID := o.ProjectID.Any()
id := o.ID.Current()

if id == "" {
return nil
}

obj, err := cli.GetNotificationChannel(ctx, &monitoring.GetNotificationChannelRequest{
Name: id,
})
if ErrIs404(err) {
o.MarkAsNew()

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

o.MarkAsExisting()
o.ProjectID.SetCurrent(projectID)
o.DisplayName.SetCurrent(obj.DisplayName)
o.Type.SetCurrent(obj.Type)

labels := make(map[string]interface{}, len(obj.Labels))

for k, v := range obj.Labels {
labels[k] = v
}

o.Labels.SetCurrent(labels)

return nil
}

func (o *NotificationChannel) createNotificationChannel(update bool) *monitoring.NotificationChannel {
displayName := o.DisplayName.Wanted()
typ := o.Type.Wanted()

labels := o.Labels.Wanted()
labelsMap := make(map[string]string, len(labels))

for k, v := range labels {
labelsMap[k] = v.(string)
}

cfg := &monitoring.NotificationChannel{
DisplayName: displayName,
Type: typ,
Labels: labelsMap,
}

if update {
cfg.Name = o.ID.Current()
}

return cfg
}

func (o *NotificationChannel) Create(ctx context.Context, meta interface{}) error {
pctx := meta.(*config.PluginContext)

cli, err := pctx.GCPMonitoringNotificationChannelClient(ctx)
if err != nil {
return err
}

projectID := o.ProjectID.Wanted()

obj, err := cli.CreateNotificationChannel(ctx, &monitoring.CreateNotificationChannelRequest{
Name: fmt.Sprintf("projects/%s", projectID),
NotificationChannel: o.createNotificationChannel(false),
})
if err != nil {
return err
}

o.ID.SetCurrent(obj.Name)

return err
}

func (o *NotificationChannel) Update(ctx context.Context, meta interface{}) error {
pctx := meta.(*config.PluginContext)

cli, err := pctx.GCPMonitoringNotificationChannelClient(ctx)
if err != nil {
return err
}

_, err = cli.UpdateNotificationChannel(ctx, &monitoring.UpdateNotificationChannelRequest{
NotificationChannel: o.createNotificationChannel(true),
})

return err
}

func (o *NotificationChannel) Delete(ctx context.Context, meta interface{}) error {
pctx := meta.(*config.PluginContext)

cli, err := pctx.GCPMonitoringNotificationChannelClient(ctx)
if err != nil {
return err
}

err = cli.DeleteNotificationChannel(ctx, &monitoring.DeleteNotificationChannelRequest{
Name: o.ID.Current(),
})

return err
}
Loading

0 comments on commit 7f3d24c

Please sign in to comment.