Skip to content

Commit

Permalink
Updated to support space ID and space name
Browse files Browse the repository at this point in the history
  • Loading branch information
jbristowe committed Jan 7, 2021
1 parent a47bcf6 commit d6d4576
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
33 changes: 29 additions & 4 deletions octopusdeploy/config.go
Expand Up @@ -9,9 +9,10 @@ import (

// Config holds Address and the APIKey of the Octopus Deploy server
type Config struct {
Address string
APIKey string
Space string
Address string
APIKey string
SpaceID string
SpaceName string
}

// Client returns a new Octopus Deploy client
Expand All @@ -21,10 +22,34 @@ func (c *Config) Client() (*octopusdeploy.Client, diag.Diagnostics) {
return nil, diag.FromErr(err)
}

client, err := octopusdeploy.NewClient(nil, apiURL, c.APIKey, c.Space)
client, err := octopusdeploy.NewClient(nil, apiURL, c.APIKey, "")
if err != nil {
return nil, diag.FromErr(err)
}

if len(c.SpaceID) > 0 {
space, err := client.Spaces.GetByID(c.SpaceID)
if err != nil {
return nil, diag.FromErr(err)
}

client, err = octopusdeploy.NewClient(nil, apiURL, c.APIKey, space.GetID())
if err != nil {
return nil, diag.FromErr(err)
}
}

if len(c.SpaceName) > 0 {
space, err := client.Spaces.GetByName(c.SpaceName)
if err != nil {
return nil, diag.FromErr(err)
}

client, err = octopusdeploy.NewClient(nil, apiURL, c.APIKey, space.GetID())
if err != nil {
return nil, diag.FromErr(err)
}
}

return client, nil
}
30 changes: 19 additions & 11 deletions octopusdeploy/provider.go
Expand Up @@ -79,22 +79,29 @@ func Provider() *schema.Provider {
},
Schema: map[string]*schema.Schema{
"address": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("OCTOPUS_URL", nil),
Description: "The endpoint of the Octopus REST API",
Required: true,
Type: schema.TypeString,
},
"api_key": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("OCTOPUS_APIKEY", nil),
Description: "The API key to use with the Octopus REST API",
Required: true,
Type: schema.TypeString,
},
"space_id": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("OCTOPUS_SPACE", ""),
Description: "The space ID to target",
ConflictsWith: []string{"space_name"},
Description: "The space ID to target",
Optional: true,
Type: schema.TypeString,
},
"space_name": {
ConflictsWith: []string{"space_id"},
DefaultFunc: schema.EnvDefaultFunc("OCTOPUS_SPACE", ""),
Description: "The space name to target",
Optional: true,
Type: schema.TypeString,
},
},

Expand All @@ -104,9 +111,10 @@ func Provider() *schema.Provider {

func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
config := Config{
Address: d.Get("address").(string),
APIKey: d.Get("api_key").(string),
Space: d.Get("space_id").(string),
Address: d.Get("address").(string),
APIKey: d.Get("api_key").(string),
SpaceID: d.Get("space_id").(string),
SpaceName: d.Get("space_name").(string),
}

return config.Client()
Expand Down

0 comments on commit d6d4576

Please sign in to comment.