-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51ec30c
commit 7990610
Showing
9 changed files
with
320 additions
and
3 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,65 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "octopusdeploy_git_trigger Resource - terraform-provider-octopusdeploy" | ||
subcategory: "" | ||
description: |- | ||
This resource manages Git repository triggers in Octopus Deploy. | ||
--- | ||
|
||
# octopusdeploy_git_trigger (Resource) | ||
|
||
This resource manages Git repository triggers in Octopus Deploy. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "octopusdeploy_git_trigger" "my_trigger" { | ||
name = "My Git trigger" | ||
space_id = "Spaces-1" | ||
project_id = "Projects-1" | ||
channel_id = "Channels-1" | ||
sources { | ||
deployment_action_slug = "deploy-action-slug" | ||
git_dependency_name = "" | ||
include_file_paths = ["include/me", "include/me/too"] | ||
exclude_file_paths = ["exclude/me", "exclude/me/too"] | ||
} | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `channel_id` (String) The ID of the channel in which the release will be created if the action type is CreateRelease. | ||
- `name` (String) The name of this resource. | ||
- `project_id` (String) The ID of the project to attach the trigger. | ||
|
||
### Optional | ||
|
||
- `is_disabled` (Boolean) Disables the trigger from being run when set. | ||
- `sources` (Block List) List of Git trigger sources. Contains details of the deployment action slug, the git dependency and what file paths to monitor. (see [below for nested schema](#nestedblock--sources)) | ||
- `space_id` (String) The space ID associated with the project to attach the trigger. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
|
||
<a id="nestedblock--sources"></a> | ||
### Nested Schema for `sources` | ||
|
||
Required: | ||
|
||
- `deployment_action_slug` (String) | ||
- `exclude_file_paths` (List of String) | ||
- `git_dependency_name` (String) | ||
- `include_file_paths` (List of String) | ||
|
||
## Import | ||
|
||
Import is supported using the following syntax: | ||
|
||
```shell | ||
terraform import [options] octopusdeploy_git_trigger.<name> <trigger-id> | ||
``` |
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 @@ | ||
terraform import [options] octopusdeploy_git_trigger.<name> <trigger-id> |
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,12 @@ | ||
resource "octopusdeploy_git_trigger" "my_trigger" { | ||
name = "My Git trigger" | ||
space_id = "Spaces-1" | ||
project_id = "Projects-1" | ||
channel_id = "Channels-1" | ||
sources { | ||
deployment_action_slug = "deploy-action-slug" | ||
git_dependency_name = "" | ||
include_file_paths = ["include/me", "include/me/too"] | ||
exclude_file_paths = ["exclude/me", "exclude/me/too"] | ||
} | ||
} |
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,130 @@ | ||
package octopusdeploy | ||
|
||
import ( | ||
"context" | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects" | ||
"log" | ||
|
||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/actions" | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client" | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/filters" | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/triggers" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func resourceGitTrigger() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceGitTriggerCreate, | ||
DeleteContext: resourceGitTriggerDelete, | ||
Description: "This resource manages Git repository triggers in Octopus Deploy.", | ||
Importer: getImporter(), | ||
ReadContext: resourceGitTriggerRead, | ||
Schema: getGitTriggerSchema(), | ||
UpdateContext: resourceGitTriggerUpdate, | ||
} | ||
} | ||
|
||
func buildGitTriggerResource(d *schema.ResourceData, client *client.Client) (*triggers.ProjectTrigger, error) { | ||
name := d.Get("name").(string) | ||
spaceId := d.Get("space_id").(string) | ||
projectId := d.Get("project_id").(string) | ||
channelId := d.Get("channel_id").(string) | ||
|
||
isDisabled := false | ||
if v, ok := d.GetOk("is_disabled"); ok { | ||
isDisabled = v.(bool) | ||
} | ||
|
||
flattenedGitTriggerSources := d.Get("sources") | ||
gitTriggerSources := expandGitTriggerSources(flattenedGitTriggerSources) | ||
|
||
action := actions.NewCreateReleaseAction(channelId) | ||
filter := filters.NewGitTriggerFilter(gitTriggerSources) | ||
|
||
project, err := projects.GetByID(client, spaceId, projectId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
createReleaseTrigger := triggers.NewProjectTrigger(name, "", isDisabled, project, action, filter) | ||
|
||
return createReleaseTrigger, nil | ||
} | ||
|
||
func resourceGitTriggerCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(*client.Client) | ||
|
||
projectTrigger, err := buildGitTriggerResource(d, client) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
resource, err := client.ProjectTriggers.Add(projectTrigger) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
if isEmpty(resource.GetID()) { | ||
log.Println("ID is nil") | ||
} else { | ||
d.SetId(resource.GetID()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceGitTriggerRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
id := d.Id() | ||
|
||
client := m.(*client.Client) | ||
projectTrigger, err := client.ProjectTriggers.GetByID(id) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if projectTrigger == nil { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
action := projectTrigger.Action.(*actions.CreateReleaseAction) | ||
filter := projectTrigger.Filter.(*filters.GitTriggerFilter) | ||
|
||
d.Set("name", projectTrigger.Name) | ||
d.Set("space_id", projectTrigger.SpaceID) | ||
d.Set("project_id", projectTrigger.ProjectID) | ||
d.Set("is_disabled", projectTrigger.IsDisabled) | ||
d.Set("channel_id", action.ChannelID) | ||
d.Set("sources", flattenGitTriggerSources(filter.Sources)) | ||
|
||
return nil | ||
} | ||
|
||
func resourceGitTriggerUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(*client.Client) | ||
projectTrigger, err := buildGitTriggerResource(d, client) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
projectTrigger.ID = d.Id() // set ID so Octopus API knows which project trigger to update | ||
|
||
resource, err := client.ProjectTriggers.Update(projectTrigger) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId(resource.GetID()) | ||
|
||
return nil | ||
} | ||
|
||
func resourceGitTriggerDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(*client.Client) | ||
err := client.ProjectTriggers.DeleteByID(d.Id()) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId("") | ||
return nil | ||
} |
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,42 @@ | ||
package octopusdeploy | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
func getGitTriggerSchema() map[string]*schema.Schema { | ||
return map[string]*schema.Schema{ | ||
"name": getNameSchema(true), | ||
"space_id": { | ||
Optional: true, | ||
Description: "The space ID associated with the project to attach the trigger.", | ||
Type: schema.TypeString, | ||
ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotWhiteSpace), | ||
}, | ||
"project_id": { | ||
Description: "The ID of the project to attach the trigger.", | ||
Required: true, | ||
Type: schema.TypeString, | ||
ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotWhiteSpace), | ||
}, | ||
"channel_id": { | ||
Description: "The ID of the channel in which the release will be created if the action type is CreateRelease.", | ||
Required: true, | ||
Type: schema.TypeString, | ||
ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotWhiteSpace), | ||
}, | ||
"sources": { | ||
Description: "List of Git trigger sources. Contains details of the deployment action slug, the git dependency and what file paths to monitor.", | ||
Optional: true, | ||
Type: schema.TypeList, | ||
Elem: &schema.Resource{Schema: getGitTriggerSourceSchema()}, | ||
}, | ||
"is_disabled": { | ||
Description: "Disables the trigger from being run when set.", | ||
Optional: true, | ||
Default: false, | ||
Type: schema.TypeBool, | ||
}, | ||
} | ||
} |
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,66 @@ | ||
package octopusdeploy | ||
|
||
import ( | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/filters" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func expandGitTriggerSources(values interface{}) []filters.GitTriggerSource { | ||
if values == nil { | ||
return nil | ||
} | ||
|
||
var gitTriggerSources []filters.GitTriggerSource | ||
for _, v := range values.([]interface{}) { | ||
flattenedMap := v.(map[string]interface{}) | ||
gitTriggerSources = append(gitTriggerSources, filters.GitTriggerSource{ | ||
DeploymentActionSlug: flattenedMap["deployment_action_slug"].(string), | ||
GitDependencyName: flattenedMap["git_dependency_name"].(string), | ||
IncludeFilePaths: flattenedMap["include_file_paths"].([]string), | ||
ExcludeFilePaths: flattenedMap["exclude_file_paths"].([]string), | ||
}) | ||
} | ||
|
||
return gitTriggerSources | ||
} | ||
|
||
func flattenGitTriggerSources(gitTriggerSources []filters.GitTriggerSource) []interface{} { | ||
if len(gitTriggerSources) == 0 { | ||
return nil | ||
} | ||
|
||
flattenedGitTriggerSources := []interface{}{} | ||
for _, v := range gitTriggerSources { | ||
flattenedGitTriggerSources = append(flattenedGitTriggerSources, map[string]interface{}{ | ||
"deployment_action_slug": v.DeploymentActionSlug, | ||
"git_dependency_name": v.GitDependencyName, | ||
"include_file_paths": v.IncludeFilePaths, | ||
"exclude_file_paths": v.ExcludeFilePaths, | ||
}) | ||
} | ||
|
||
return flattenedGitTriggerSources | ||
} | ||
|
||
func getGitTriggerSourceSchema() map[string]*schema.Schema { | ||
return map[string]*schema.Schema{ | ||
"deployment_action_slug": { | ||
Required: true, | ||
Type: schema.TypeString, | ||
}, | ||
"git_dependency_name": { | ||
Required: true, | ||
Type: schema.TypeString, | ||
}, | ||
"include_file_paths": { | ||
Required: true, | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"exclude_file_paths": { | ||
Required: true, | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
} | ||
} |