-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a kubernetes style resource async class
Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
4beb716
commit 9c2dc9e
Showing
60 changed files
with
421 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package google | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
const readyStatus string = "Ready" | ||
|
||
type Condition struct { | ||
Type string | ||
Status string | ||
Reason string | ||
Message string | ||
} | ||
|
||
// KnativeStatus is a struct that can contain a Knative style resource's Status block. It is not | ||
// intended to be used for anything other than polling for the success of the given resource. | ||
type KnativeStatus struct { | ||
Metadata struct { | ||
Name string | ||
Namespace string | ||
SelfLink string | ||
} | ||
Status struct { | ||
Conditions []Condition | ||
} | ||
} | ||
|
||
// ConditionByType is a helper method for extracting a given condition | ||
func (s KnativeStatus) ConditionByType(typ string) *Condition { | ||
for _, condition := range s.Status.Conditions { | ||
if condition.Type == typ { | ||
c := condition | ||
return &c | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// LatestMessage will return a human consumable status of the resource. This can | ||
// be used to determine the human actionable error the GET doesn't return an explicit | ||
// error but the resource is in an error state. | ||
func (s KnativeStatus) LatestMessage() string { | ||
c := s.ConditionByType(readyStatus) | ||
if c != nil { | ||
return fmt.Sprintf("%s - %s", c.Reason, c.Message) | ||
} | ||
|
||
return "" | ||
} | ||
|
||
// State will return a string representing the status of the Ready condition. | ||
// No other conditions are currently returned as part of the state. | ||
func (s KnativeStatus) State(res interface{}) string { | ||
for _, condition := range s.Status.Conditions { | ||
if condition.Type == "Ready" { | ||
return fmt.Sprintf("%s:%s", condition.Type, condition.Status) | ||
} | ||
} | ||
return "Empty" | ||
} | ||
|
||
// CloudRunPolling allows for polling against a cloud run resource that implements the | ||
// Kubernetes style status schema. | ||
type CloudRunPolling struct { | ||
Config *Config | ||
WaitURL string | ||
} | ||
|
||
func (p *CloudRunPolling) PendingStates() []string { | ||
return []string{"Ready:Unknown", "Empty"} | ||
} | ||
func (p *CloudRunPolling) TargetStates() []string { | ||
return []string{"Ready:True"} | ||
} | ||
func (p *CloudRunPolling) ErrorStates() []string { | ||
return []string{"Ready:False"} | ||
} | ||
|
||
func cloudRunPollingWaitTime(config *Config, res map[string]interface{}, project, url, activity string, timeoutMinutes int) error { | ||
w := &CloudRunPolling{} | ||
|
||
scc := &resource.StateChangeConf{ | ||
Pending: w.PendingStates(), | ||
Target: w.TargetStates(), | ||
Refresh: func() (interface{}, string, error) { | ||
res, err := sendRequest(config, "GET", project, url, nil) | ||
if err != nil { | ||
return res, "", err | ||
} | ||
|
||
status := KnativeStatus{} | ||
err = Convert(res, &status) | ||
if err != nil { | ||
return res, "", err | ||
} | ||
|
||
for _, errState := range w.ErrorStates() { | ||
if status.State(res) == errState { | ||
err = errors.New(status.LatestMessage()) | ||
} | ||
} | ||
|
||
return res, status.State(res), err | ||
}, | ||
Timeout: time.Duration(timeoutMinutes) * time.Minute, | ||
} | ||
|
||
_, err := scc.WaitForState() | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// ---------------------------------------------------------------------------- | ||
// | ||
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** | ||
// | ||
// ---------------------------------------------------------------------------- | ||
// | ||
// This file is automatically generated by Magic Modules and manual | ||
// changes will be clobbered when the file is regenerated. | ||
// | ||
// Please read more about how to change this file in | ||
// .github/CONTRIBUTING.md. | ||
// | ||
// ---------------------------------------------------------------------------- | ||
|
||
package google | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
func TestAccCloudRunService_cloudRunServiceBasicExample(t *testing.T) { | ||
t.Parallel() | ||
|
||
context := map[string]interface{}{ | ||
"namespace": getTestProjectFromEnv(), | ||
"random_suffix": acctest.RandString(10), | ||
} | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckCloudRunServiceDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCloudRunService_cloudRunServiceBasicExample(context), | ||
}, | ||
{ | ||
ResourceName: "google_cloud_run_service.default", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCloudRunService_cloudRunServiceBasicExample(context map[string]interface{}) string { | ||
return Nprintf(` | ||
resource "google_cloud_run_service" "default" { | ||
name = "tftest-cloudrun%{random_suffix}" | ||
location = "us-central1" | ||
metadata { | ||
namespace = "%{namespace}" | ||
} | ||
template { | ||
spec { | ||
containers { | ||
image = "gcr.io/cloudrun/hello" | ||
} | ||
} | ||
} | ||
traffic { | ||
percent = 100 | ||
latest_revision = true | ||
} | ||
} | ||
`, context) | ||
} | ||
|
||
func testAccCheckCloudRunServiceDestroy(s *terraform.State) error { | ||
for name, rs := range s.RootModule().Resources { | ||
if rs.Type != "google_cloud_run_service" { | ||
continue | ||
} | ||
if strings.HasPrefix(name, "data.") { | ||
continue | ||
} | ||
|
||
config := testAccProvider.Meta().(*Config) | ||
|
||
url, err := replaceVarsForTest(config, rs, "{{CloudRunBasePath}}serving.knative.dev/v1/namespaces/{{project}}/services/{{name}}") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = sendRequest(config, "GET", "", url, nil) | ||
if err == nil { | ||
return fmt.Errorf("CloudRunService still exists at %s", url) | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.