deploy: Add ability to invalidate Google Cloud CDN
authorGarret Kelly <gdk@google.com>
Thu, 3 Oct 2019 12:46:27 +0000 (08:46 -0400)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 3 Oct 2019 14:53:45 +0000 (16:53 +0200)
commands/deploy.go
deploy/deploy.go
deploy/deployConfig.go
deploy/google.go [new file with mode: 0644]
go.mod

index d4b04ab78aa9f9dfe2e415731ec76a3cf593d91f..d74d93709c4948ce06a90ee967e4012cbbb84be5 100644 (file)
@@ -70,7 +70,7 @@ documentation.
        cc.cmd.Flags().Bool("confirm", false, "ask for confirmation before making changes to the target")
        cc.cmd.Flags().Bool("dryRun", false, "dry run")
        cc.cmd.Flags().Bool("force", false, "force upload of all files")
-       cc.cmd.Flags().Bool("invalidateCDN", true, "invalidate the CDN cache via the cloudFrontDistributionID listed in the deployment target")
+       cc.cmd.Flags().Bool("invalidateCDN", true, "invalidate the CDN cache listed in the deployment target")
        cc.cmd.Flags().Int("maxDeletes", 256, "maximum # of files to delete, or -1 to disable")
 
        return cc
index 9092431a597d479a7089b7da2860adfa4289fd75..1d911f29b83972f2eed7ca7dd0f12c5e2c95d1c1 100644 (file)
@@ -249,11 +249,20 @@ func (d *Deployer) Deploy(ctx context.Context) error {
                jww.FEEDBACK.Println("Success!")
        }
 
-       if d.invalidateCDN && d.target.CloudFrontDistributionID != "" {
-               jww.FEEDBACK.Println("Invalidating CloudFront CDN...")
-               if err := InvalidateCloudFront(ctx, d.target.CloudFrontDistributionID); err != nil {
-                       jww.FEEDBACK.Printf("Failed to invalidate CloudFront CDN: %v\n", err)
-                       return err
+       if d.invalidateCDN {
+               if d.target.CloudFrontDistributionID != "" {
+                       jww.FEEDBACK.Println("Invalidating CloudFront CDN...")
+                       if err := InvalidateCloudFront(ctx, d.target.CloudFrontDistributionID); err != nil {
+                               jww.FEEDBACK.Printf("Failed to invalidate CloudFront CDN: %v\n", err)
+                               return err
+                       }
+               }
+               if d.target.GoogleCloudCDNOrigin != "" {
+                       jww.FEEDBACK.Println("Invalidating Google Cloud CDN...")
+                       if err := InvalidateGoogleCloudCDN(ctx, d.target.GoogleCloudCDNOrigin); err != nil {
+                               jww.FEEDBACK.Printf("Failed to invalidate Google Cloud CDN: %v\n", err)
+                               return err
+                       }
                }
                jww.FEEDBACK.Println("Success!")
        }
index 0ea675b826457505a17590c66aee0dbb0db79783..3bc51294d75094b80aa4bfe28bffb756ad0484b7 100644 (file)
@@ -37,6 +37,10 @@ type target struct {
        URL  string
 
        CloudFrontDistributionID string
+
+       // GoogleCloudCDNOrigin specifies the Google Cloud project and CDN origin to
+       // invalidate when deploying this target.  It is specified as <project>/<origin>.
+       GoogleCloudCDNOrigin string
 }
 
 // matcher represents configuration to be applied to files whose paths match
diff --git a/deploy/google.go b/deploy/google.go
new file mode 100644 (file)
index 0000000..be3ce52
--- /dev/null
@@ -0,0 +1,37 @@
+// Copyright 2019 The Hugo Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package deploy
+
+import (
+       "context"
+       "fmt"
+       "strings"
+
+       "google.golang.org/api/compute/v1"
+)
+
+// Invalidate all of the content in a Google Cloud CDN distribution.
+func InvalidateGoogleCloudCDN(ctx context.Context, origin string) error {
+       parts := strings.Split(origin, "/")
+       if len(parts) != 2 {
+               return fmt.Errorf("origin must be <project>/<origin>")
+       }
+       service, err := compute.NewService(ctx)
+       if err != nil {
+               return err
+       }
+       rule := &compute.CacheInvalidationRule{Path: "/*"}
+       _, err = service.UrlMaps.InvalidateCache(parts[0], parts[1], rule).Context(ctx).Do()
+       return err
+}
diff --git a/go.mod b/go.mod
index fabb14101c11784962daa67a3387ec3d64d8264e..ce831ac90ac9071fef4dea2c3ce82af206de153f 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -63,6 +63,7 @@ require (
        golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7 // indirect
        golang.org/x/text v0.3.2
        golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 // indirect
+       google.golang.org/api v0.5.0
        google.golang.org/appengine v1.6.0 // indirect
        google.golang.org/genproto v0.0.0-20190522204451-c2c4e71fbf69 // indirect
        gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect