Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable canar #29

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spinnaker/api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func GetApplication(client *gate.GatewayClient, applicationName string, dest int
return nil
}

func CreateApplication(client *gate.GatewayClient, applicationName, email string) error {
func CreateApplication(client *gate.GatewayClient, applicationName, email string, canary bool) error {

app := map[string]interface{}{
"instancePort": 80,
Expand Down
37 changes: 37 additions & 0 deletions spinnaker/api/task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package api

import (
"fmt"
"net/http"

gate "github.com/spinnaker/spin/cmd/gateclient"
)

func EnableApplicationCanary(client *gate.GatewayClient, applicationName string) error {
jobSpec := map[string]interface{}{
"type": "updateApplication",
"application": map[string]interface{}{
"name": applicationName,
"datasources": map[string]interface{}{
"enabled": "canaryConfigs",
"disabled": "",
},
"user": "[anonymous]",
},
}
enableAppCanaryTask := map[string]interface{}{
"job": []interface{}{jobSpec},
"application": applicationName,
"description": fmt.Sprintf("Enable canary for: %s", applicationName),
}

_, resp, err := client.TaskControllerApi.TaskUsingPOST1(client.Context, enableAppCanaryTask)

if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Encountered an error enabling canary, status code: %d\n", resp.StatusCode)
}
return nil
}
13 changes: 12 additions & 1 deletion spinnaker/resource_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func resourceApplication() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"canary": {
Type: schema.TypeBool,
Optional: true,
},
},
Create: resourceApplicationCreate,
Read: resourceApplicationRead,
Expand All @@ -39,10 +43,17 @@ func resourceApplicationCreate(data *schema.ResourceData, meta interface{}) erro
client := clientConfig.client
application := data.Get("application").(string)
email := data.Get("email").(string)
canary := data.Get("canary").(bool)

if err := api.CreateApplication(client, application, email); err != nil {
if err := api.CreateApplication(client, application, email, canary); err != nil {
return err
}
if canary {
err := api.EnableApplicationCanary(client, application)
if err != nil {
return err
}
}

return resourceApplicationRead(data, meta)
}
Expand Down
2 changes: 2 additions & 0 deletions spinnaker/resource_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestAccSpinnakerApplication_basic(t *testing.T) {
testAccCheckApplicationExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "application", rName),
resource.TestCheckResourceAttr(resourceName, "email", "[email protected]"),
resource.TestCheckResourceAttr(resourceName, "canary", "true"),
),
},
},
Expand Down Expand Up @@ -67,6 +68,7 @@ func testAccSpinnakerApplication_basic(rName string) string {
resource "spinnaker_application" "test" {
application = %q
email = "[email protected]"
canary = true
}
`, rName)
}