Skip to content

Commit

Permalink
More code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jbristowe committed Sep 27, 2020
1 parent a51fb6d commit 2eeb116
Show file tree
Hide file tree
Showing 19 changed files with 180 additions and 159 deletions.
2 changes: 2 additions & 0 deletions octopusdeploy/constants.go
Expand Up @@ -12,6 +12,7 @@ const (
constAcquisitionLocation string = "acquisition_location"
constAddress string = "address"
constAllowDynamicInfrastructure string = "allow_dynamic_infrastructure"
constAllowDeploymentsToNoTargets string = "allow_deployments_to_no_targets"
constAPIKey string = "apikey"
constApplyTerraformAction string = "apply_terraform_action"
constArguments string = "arguments"
Expand Down Expand Up @@ -136,6 +137,7 @@ const (
constStartMode string = "start_mode"
constStartTrigger string = "start_trigger"
constStatus string = "status"
constStep string = "step"
constSubscriptionNumber string = "subscription_number"
constTag string = "tag"
constTagSet string = "tag_set"
Expand Down
31 changes: 17 additions & 14 deletions octopusdeploy/deployment_process.go
@@ -1,7 +1,7 @@
package octopusdeploy

import (
"fmt"
"log"

"github.com/OctopusDeploy/go-octopusdeploy/client"
"github.com/OctopusDeploy/go-octopusdeploy/model"
Expand All @@ -20,35 +20,38 @@ func resourceDeploymentProcess() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"step": getDeploymentStepSchema(),
constStep: getDeploymentStepSchema(),
},
}
}

func resourceDeploymentProcessCreate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

newDeploymentProcess := buildDeploymentProcessResource(d)
deploymentProcess := buildDeploymentProcessResource(d)

project, err := apiClient.Projects.GetByID(newDeploymentProcess.ProjectID)
apiClient := m.(*client.Client)
project, err := apiClient.Projects.GetByID(deploymentProcess.ProjectID)
if err != nil {
return fmt.Errorf("error getting project %s: %s", project.Name, err.Error())
return createResourceOperationError(errorReadingProject, project.Name, err)
}

current, err := apiClient.DeploymentProcesses.GetByID(project.DeploymentProcessID)
if err != nil {
return fmt.Errorf("error getting deployment process for %s: %s", project.Name, err.Error())
return createResourceOperationError(errorReadingDeploymentProcess, project.DeploymentProcessID, err)
}

newDeploymentProcess.ID = current.ID
newDeploymentProcess.Version = current.Version
createdDeploymentProcess, err := apiClient.DeploymentProcesses.Update(*newDeploymentProcess)
deploymentProcess.ID = current.ID
deploymentProcess.Version = current.Version

resource, err := apiClient.DeploymentProcesses.Update(*deploymentProcess)
if err != nil {
return fmt.Errorf("error creating deployment process: %s", err.Error())
return createResourceOperationError(errorCreatingDeploymentProcess, deploymentProcess.ID, err)
}

d.SetId(createdDeploymentProcess.ID)
if isEmpty(resource.ID) {
log.Println("ID is nil")
} else {
d.SetId(resource.ID)
}

return nil
}
Expand All @@ -58,7 +61,7 @@ func buildDeploymentProcessResource(d *schema.ResourceData) *model.DeploymentPro
ProjectID: d.Get(constProjectID).(string),
}

if attr, ok := d.GetOk("step"); ok {
if attr, ok := d.GetOk(constStep); ok {
tfSteps := attr.([]interface{})

for _, tfStep := range tfSteps {
Expand Down
21 changes: 6 additions & 15 deletions octopusdeploy/resource_aws_account.go
Expand Up @@ -91,31 +91,22 @@ func buildAmazonWebServicesAccountResource(d *schema.ResourceData) (*model.Accou
}

func resourceAmazonWebServicesAccountCreate(d *schema.ResourceData, m interface{}) error {
if d == nil {
return createInvalidParameterError("resourceAmazonWebServicesAccountCreate", "d")
}

if m == nil {
return createInvalidParameterError("resourceAmazonWebServicesAccountCreate", "m")
}

apiClient := m.(*client.Client)

newAccount, err := buildAmazonWebServicesAccountResource(d)
account, err := buildAmazonWebServicesAccountResource(d)
if err != nil {
log.Println(err)
return err
}

account, err := apiClient.Accounts.Add(newAccount)
apiClient := m.(*client.Client)
resource, err := apiClient.Accounts.Add(account)
if err != nil {
return createResourceOperationError(errorCreatingAWSAccount, newAccount.Name, err)
return createResourceOperationError(errorCreatingAWSAccount, account.Name, err)
}

if account.ID == constEmptyString {
if isEmpty(resource.ID) {
log.Println("ID is nil")
} else {
d.SetId(account.ID)
d.SetId(resource.ID)
}

return nil
Expand Down
21 changes: 6 additions & 15 deletions octopusdeploy/resource_azure_service_principal.go
Expand Up @@ -134,31 +134,22 @@ func buildAzureServicePrincipalResource(d *schema.ResourceData) (*model.Account,
}

func resourceAzureServicePrincipalCreate(d *schema.ResourceData, m interface{}) error {
if d == nil {
return createInvalidParameterError("resourceAzureServicePrincipalRead", "d")
}

if m == nil {
return createInvalidParameterError("resourceAzureServicePrincipalRead", "m")
}

apiClient := m.(*client.Client)

newAccount, err := buildAzureServicePrincipalResource(d)
account, err := buildAzureServicePrincipalResource(d)
if err != nil {
log.Println(err)
return err
}
account, err := apiClient.Accounts.Add(newAccount)

apiClient := m.(*client.Client)
resource, err := apiClient.Accounts.Add(account)
if err != nil {
createResourceOperationError(errorCreatingAzureServicePrincipal, newAccount.Name, err)
createResourceOperationError(errorCreatingAzureServicePrincipal, account.Name, err)
}

if account.ID == constEmptyString {
if isEmpty(resource.ID) {
log.Println("ID is nil")
} else {
d.SetId(account.ID)
d.SetId(resource.ID)
}

return nil
Expand Down
12 changes: 6 additions & 6 deletions octopusdeploy/resource_certificate.go
Expand Up @@ -123,21 +123,21 @@ func buildCertificateResource(d *schema.ResourceData) (*model.Certificate, error
}

func resourceCertificateCreate(d *schema.ResourceData, m interface{}) error {
newCertificate, err := buildCertificateResource(d)
certificate, err := buildCertificateResource(d)
if err != nil {
return err
}

apiClient := m.(*client.Client)
certificate, err := apiClient.Certificates.Add(newCertificate)
resource, err := apiClient.Certificates.Add(certificate)
if err != nil {
return createResourceOperationError(errorCreatingCertificate, newCertificate.Name, err)
return createResourceOperationError(errorCreatingCertificate, certificate.Name, err)
}

if isEmpty(certificate.ID) {
log.Println("ID is empty")
if isEmpty(resource.ID) {
log.Println("ID is nil")
} else {
d.SetId(certificate.ID)
d.SetId(resource.ID)
}

return nil
Expand Down
17 changes: 10 additions & 7 deletions octopusdeploy/resource_channel.go
@@ -1,7 +1,7 @@
package octopusdeploy

import (
"fmt"
"log"

"github.com/OctopusDeploy/go-octopusdeploy/client"
"github.com/OctopusDeploy/go-octopusdeploy/model"
Expand Down Expand Up @@ -62,16 +62,19 @@ func resourceChannel() *schema.Resource {
}

func resourceChannelCreate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

newChannel := buildChannelResource(d)
channel, err := apiClient.Channels.Add(newChannel)
channel := buildChannelResource(d)

apiClient := m.(*client.Client)
resource, err := apiClient.Channels.Add(channel)
if err != nil {
return fmt.Errorf("error creating channel %s: %s", newChannel.Name, err.Error())
return createResourceOperationError(errorCreatingChannel, channel.Name, err)
}

d.SetId(channel.ID)
if isEmpty(resource.ID) {
log.Println("ID is nil")
} else {
d.SetId(resource.ID)
}

return nil
}
Expand Down
16 changes: 10 additions & 6 deletions octopusdeploy/resource_environment.go
Expand Up @@ -2,6 +2,7 @@ package octopusdeploy

import (
"fmt"
"log"

"github.com/OctopusDeploy/go-octopusdeploy/client"
"github.com/OctopusDeploy/go-octopusdeploy/model"
Expand Down Expand Up @@ -93,16 +94,19 @@ func buildEnvironmentResource(d *schema.ResourceData) *model.Environment {
}

func resourceEnvironmentCreate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)

newEnvironment := buildEnvironmentResource(d)
env, err := apiClient.Environments.Add(newEnvironment)
environment := buildEnvironmentResource(d)

apiClient := m.(*client.Client)
resource, err := apiClient.Environments.Add(environment)
if err != nil {
return fmt.Errorf("error creating environment %s: %s", newEnvironment.Name, err.Error())
return createResourceOperationError(errorCreatingEnvironment, environment.Name, err)
}

d.SetId(env.ID)
if isEmpty(resource.ID) {
log.Println("ID is nil")
} else {
d.SetId(resource.ID)
}

return nil
}
Expand Down
13 changes: 9 additions & 4 deletions octopusdeploy/resource_feed.go
Expand Up @@ -2,6 +2,7 @@ package octopusdeploy

import (
"fmt"
"log"

"github.com/OctopusDeploy/go-octopusdeploy/client"
"github.com/OctopusDeploy/go-octopusdeploy/enum"
Expand Down Expand Up @@ -130,15 +131,19 @@ func buildFeedResource(d *schema.ResourceData) *model.Feed {
}

func resourceFeedCreate(d *schema.ResourceData, m interface{}) error {
resource := buildFeedResource(d)
feed := buildFeedResource(d)

apiClient := m.(*client.Client)
feed, err := apiClient.Feeds.Add(*resource)
resource, err := apiClient.Feeds.Add(*feed)
if err != nil {
return fmt.Errorf(errorCreatingFeed, resource.Name, err.Error())
return createResourceOperationError(errorCreatingFeed, feed.Name, err)
}

d.SetId(feed.ID)
if isEmpty(resource.ID) {
log.Println("ID is nil")
} else {
d.SetId(resource.ID)
}

return nil
}
Expand Down
24 changes: 10 additions & 14 deletions octopusdeploy/resource_library_variable_set.go
@@ -1,6 +1,8 @@
package octopusdeploy

import (
"log"

"github.com/OctopusDeploy/go-octopusdeploy/client"
"github.com/OctopusDeploy/go-octopusdeploy/model"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -43,25 +45,19 @@ func getTemplatesSchema() *schema.Schema {
}

func resourceLibraryVariableSetCreate(d *schema.ResourceData, m interface{}) error {
if d == nil {
return createInvalidParameterError("resourceLibraryVariableSetCreate", "d")
}

if m == nil {
return createInvalidParameterError("resourceLibraryVariableSetCreate", "m")
}
libraryVariableSet := buildLibraryVariableSetResource(d)

apiClient := m.(*client.Client)

newLibraryVariableSet := buildLibraryVariableSetResource(d)

createdLibraryVariableSet, err := apiClient.LibraryVariableSets.Add(newLibraryVariableSet)

resource, err := apiClient.LibraryVariableSets.Add(libraryVariableSet)
if err != nil {
return createResourceOperationError(errorCreatingLibraryVariableSet, newLibraryVariableSet.Name, err)
return createResourceOperationError(errorCreatingLibraryVariableSet, libraryVariableSet.Name, err)
}

d.SetId(createdLibraryVariableSet.ID)
if isEmpty(resource.ID) {
log.Println("ID is nil")
} else {
d.SetId(resource.ID)
}

return nil
}
Expand Down
25 changes: 11 additions & 14 deletions octopusdeploy/resource_lifecycle.go
@@ -1,6 +1,8 @@
package octopusdeploy

import (
"log"

"github.com/OctopusDeploy/go-octopusdeploy/client"
"github.com/OctopusDeploy/go-octopusdeploy/model"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -103,27 +105,22 @@ func getPhasesSchema() *schema.Schema {
}

func resourceLifecycleCreate(d *schema.ResourceData, m interface{}) error {
if d == nil {
return createInvalidParameterError("resourceLifecycleCreate", "d")
}

if m == nil {
return createInvalidParameterError("resourceLifecycleCreate", "m")
}

apiClient := m.(*client.Client)

newResource, err := buildLifecycleResource(d)
lifecycle, err := buildLifecycleResource(d)
if err != nil {
return err
}

createdResource, err := apiClient.Lifecycles.Add(newResource)
apiClient := m.(*client.Client)
resource, err := apiClient.Lifecycles.Add(lifecycle)
if err != nil {
return createResourceOperationError(errorCreatingLifecycle, newResource.Name, err)
return createResourceOperationError(errorCreatingLifecycle, lifecycle.Name, err)
}

d.SetId(createdResource.ID)
if isEmpty(resource.ID) {
log.Println("ID is nil")
} else {
d.SetId(resource.ID)
}

return nil
}
Expand Down

0 comments on commit 2eeb116

Please sign in to comment.