Skip to content

Commit

Permalink
Merge pull request #20 from OctopusDeploy/spaces
Browse files Browse the repository at this point in the history
Basic support for Spaces.
  • Loading branch information
jeff-french authored Jun 14, 2019
2 parents df90d2c + 95e9053 commit d4ca89d
Show file tree
Hide file tree
Showing 13 changed files with 333 additions and 70 deletions.
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,77 @@ To use it, extract the binary for your platform into the same folder as your `.t

## Configure the Provider

### Default Space

```hcl
# main.tf
provider "octopusdeploy" {
address = "http://octopus.production.yolo"
apikey = "API-XXXXXXXXXXXXX"
}
```

### Scoped to a single Space

Simply provide the _name_ of the space (not the space ID)

**Note:** System level resources such as Teams are not support on a Space-scoped provider.

```hcl
# main.tf
provider "octopusdeploy" {
address = "http://octopus.production.yolo"
apikey = "API-XXXXXXXXXXXXX"
space = "Support" // The name of the space
}
```

### Multiple spaces

To manage resources in multiple spaces you currently must use multiple instances of the provider with [aliases](https://www.terraform.io/docs/configuration/providers.html#alias-multiple-provider-instances) like so:

```hcl
# main.tf
provider "octopusdeploy" {
address = "http://octopus.production.yolo"
apikey = "API-XXXXXXXXXXXXX"
}
provider "octopusdeploy" {
alias = "space_support"
address = "http://octopus.production.yolo"
apikey = "API-XXXXXXXXXXXXX"
space = "Support" // The name of the space
}
provider "octopusdeploy" {
alias = "space_product1"
address = "http://octopus.production.yolo"
apikey = "API-XXXXXXXXXXXXX"
space = "Product1" // The name of another space
}
// This resource will use the default provider and the default space
resource "octopusdeploy_environment" "Env1" {
name = "TestEnv1"
}
// This resource will use the provicder aliased as "space_1" which is scoped to "Space-1"
resource "octopusdeploy_environment" "Env2" {
provider = "octopusdeploy.space_support"
name = "TestEnv2"
}
// This resource will use the provider aliased as "space_33" which is scoped to "Space-33"
resource "octopusdeploy_environment" "Env3" {
provider = "octopusdeploy.space_product1"
name = "TestEnv3"
}
```

## Data Sources
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/OctopusDeploy/terraform-provider-octopusdeploy

require (
github.com/OctopusDeploy/go-octopusdeploy v1.0.0
github.com/OctopusDeploy/go-octopusdeploy v1.1.0
github.com/apparentlymart/go-cidr v1.0.0 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/aws/aws-sdk-go v1.15.53 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
github.com/DHowett/go-plist v0.0.0-20180609054337-500bd5b9081b/go.mod h1:5paT5ZDrOm8eAJPem2Bd+q3FTi3Gxm/U4tb2tH8YIUQ=
github.com/OctopusDeploy/go-octopusdeploy v1.0.0 h1:JweXaa6zTIv8NOpLDLomzceVGV+v4L/zrOHN5DOZykc=
github.com/OctopusDeploy/go-octopusdeploy v1.0.0/go.mod h1:WyBvcyhFPMULbZwFFWOmt6/irqrfZ/WqCy7ufa8/CGE=
github.com/OctopusDeploy/go-octopusdeploy v1.1.0 h1:zVnapVTe8MDhiqEszKLlZuXVrf7fvy96d8IwgWQwPOE=
github.com/OctopusDeploy/go-octopusdeploy v1.1.0/go.mod h1:WyBvcyhFPMULbZwFFWOmt6/irqrfZ/WqCy7ufa8/CGE=
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/apparentlymart/go-cidr v1.0.0 h1:lGDvXx8Lv9QHjrAVP7jyzleG4F9+FkRhJcEsDFxeb8w=
Expand Down
28 changes: 23 additions & 5 deletions octopusdeploy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,31 @@ import (
type Config struct {
Address string
APIKey string
Space string
}

// Client returns a new Octopus Deploy client
func (c *Config) Client() *octopusdeploy.Client {
httpClient := http.Client{}
client := octopusdeploy.NewClient(&httpClient, c.Address, c.APIKey)
log.Printf("[INFO] Octopus Deploy Client configured ")
func (c *Config) Client() (*octopusdeploy.Client, error) {
client := octopusdeploy.NewClient(&(http.Client{}), c.Address, c.APIKey)

return client
if c.Space == "" {

log.Printf("[INFO] Octopus Deploy Client configured against default space")

return client, nil
}

log.Printf("[INFO] Octopus Deploy Client will be scoped to %s space", c.Space)

space, err := client.Space.GetByName(c.Space)

if err != nil {
return nil, err
}

scopedClient := octopusdeploy.ForSpace(&(http.Client{}), c.Address, c.APIKey, space)

log.Printf("[INFO] Octopus Deploy Client configured against %s space", c.Space)

return scopedClient, nil
}
11 changes: 9 additions & 2 deletions octopusdeploy/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ func Provider() terraform.ResourceProvider {
DefaultFunc: schema.EnvDefaultFunc("OCTOPUS_APIKEY", nil),
Description: "The API to use with the Octopus Deploy server.",
},
"space": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("OCTOPUS_SPACE", ""),
Description: "The name of the Space in Octopus Deploy server",
},
},

ConfigureFunc: providerConfigure,
Expand All @@ -56,10 +62,11 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{
Address: d.Get("address").(string),
APIKey: d.Get("apikey").(string),
Space: d.Get("space").(string),
}

log.Println("[INFO] Initializing Octopus Deploy client")
client := config.Client()
client, err := config.Client()

return client, nil
return client, err
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d4ca89d

Please sign in to comment.