Skip to content

Commit

Permalink
Merge pull request #252 from Cloud-SH/master
Browse files Browse the repository at this point in the history
update: sourcepipeline(trigger) resources and data
  • Loading branch information
minosmlee authored Dec 6, 2022
2 parents f9d6f3e + 566d3f7 commit cc301ea
Show file tree
Hide file tree
Showing 11 changed files with 515 additions and 106 deletions.
15 changes: 12 additions & 3 deletions docs/data-sources/sourcepipeline_project.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ The following attributes are exported:
* `full_manifest` - List of target manifest files name of the Sourcedeploy task.
* `linked_tasks` - List of linked tasks.
* `triggers`
* `sourcecommit`
* `repository_name` - Name of the Sourcecommit repository to trigger execution of pipeline.
* `branch` - Name of repository branch to trigger execution of pipeline.
* `repository` - Repository trigger.
* `type` - Type of the repository.
* `name` - Name of the repository.
* `branch` - Name of a repository branch.
* `schedule` - Schedule trigger.
* `day` - List of day of week .
* `time` - Time to trigger.
* `timezone` - Timezone for trigger.
* `execute_only_with_change` - Schedule trigger option. Schedule trigger always execute in time, if option is false. Schedule trigger execute when Sourcepipeline project configuration or Sourcecommit repository has changed, if option is true.
* `sourcepipeline` - Sourcepipeline trigger.
* `id` - Id of the sourcepipeline project to trigger execution of pipeline.
* `name` - Name of the sourcepipeline project to trigger execution of pipeline.
22 changes: 22 additions & 0 deletions docs/data-sources/sourcepipeline_trigger_timezone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Data Source: ncloud_sourcepipieline_trigger_timezone

This data source is useful for look up the list of Sourcepipeline trigger time zone.

## Example Usage

In the example below, Retrieves all Sourcepipeline schedule trigger time zone list.

```hcl
data "ncloud_sourcepipeline_trigger_timezone" "list_timezone" {
}
output "lookup-timezone-output" {
value = data.ncloud_sourcepipeline_trigger_timezone.list_timezone.timezone
}
```

## Attributes Reference

The following attributes are exported:

* `timezone` - The list of Timezone for schedule trigger.
44 changes: 39 additions & 5 deletions docs/resources/sourcepipeline_project.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,20 @@ resource "ncloud_sourcepipeline_project" "test-sourcepipeline" {
linked_tasks = ["task_name_1"]
}
triggers {
sourcecommit {
repository {
type = "sourcecommit"
repository_name = ncloud_sourcecommit_repository.test-sourcecommit.name
branch = "master"
}
schedule {
day = ["MON", "TUE"]
time = "13:01"
timezone = "Asia/Seoul (UTC+09:00)"
execute_only_with_change = false
}
sourcepipeline {
id = 1234
}
}
}
```
Expand All @@ -67,6 +77,9 @@ data "ncloud_sourcedeploy_project_stage_scenarios" "test-sourcedeploy_scenarios"
stage_id = data.ncloud_sourcedeploy_project_stages.test-sourcedeploy_stages.stages[0].id
}
data "ncloud_sourcepipeline_projects" "test-sourcepipeline" {
}
resource "ncloud_sourcepipeline_project" "test-sourcepipeline" {
name = "tf-sourcepipeline_project-test"
task {
Expand All @@ -91,10 +104,20 @@ resource "ncloud_sourcepipeline_project" "test-sourcepipeline" {
linked_tasks = ["task_name_1"]
}
triggers {
sourcecommit {
repository {
type = "sourcecommit"
repository_name = ncloud_sourcecommit_repository.test-sourcecommit.name
branch = "master"
}
schedule {
day = ["MON", "TUE"]
time = "13:01"
timezone = "Asia/Seoul (UTC+09:00)"
execute_only_with_change = false
}
sourcepipeline {
id = data.ncloud_sourcebuild_projects.test-sourcepipeline.projects[0].id
}
}
}
```
Expand All @@ -116,9 +139,17 @@ The following arguments are supported:
* `repository_branch` - (Optional) Target repository branch of SourceBuild task. Default : main branch of target repository
* `linked_tasks` - (Required) Linked tasks which has to be executed before.
* `triggers` - (Required) `triggers` block describes trigger configuration.
* `sourcecommit` - (Optional)
* `repository_name` - (Required) Name of sourcecommit repository to trigger execution of pipeline
* `branch` - (Required) Name of a repository branch to trigger execution of pipeline.
* `repository` - (Optional)
* `type` - (Optional, Required if `trigger.repository` exists) Type of repository. Accepted values: `sourcecommit`
* `name` - (Optional, Required if `trigger.repository` exists) Name of repository.
* `branch` - (Optional, Required if `trigger.repository` exists) Name of a repository branch.
* `schedule` - (Optional)
* `day` - (Optional, Required if `trigger.schedule` exists) List of day of week. Accepted values: `MON` | `TUE` | `WED` | `THU` | `FRI` | `SAT` | `SUN`.
* `time` - (Optional, Required if `trigger.schedule` exists) Time to trigger.
* `timezone` - (Optional, Required if `trigger.schedule` exists) Timezone for trigger.
* `execute_only_with_change` - (Optional, Required if `trigger.schedule` exists) Schedule trigger option. You can decide whether schedule trigger always execute in time or execute if Sourcepipeline project configuration or Sourcecommit repository has changed.
* `sourcepipeline` - (Optional)
* `id` - (Optional, Required if `trigger.sourcepipeline` exists) Id of the sourcepipeline project.

## Attributes Reference

Expand All @@ -132,3 +163,6 @@ The following arguments are supported:
* `file` - Target file name of the Sourcedeploy task. It is set only when `task.type` is SourceDeploy and `task.config.target.type` is ObjectStorage.
* `manifest` - Target manifest file name of the Sourcedeploy task. It is set only when `task.type` is SourceDeploy and `task.config.target.type` is KubernetesService.
* `full_manifest` - List of target manifest files name. It is set only when `task.type` is SourceDeploy and `task.config.target.type` is KubernetesService.
* `triggers`
* `sourcepipeline`
* `name` - Name of the sourcepipeline trigger project.
19 changes: 16 additions & 3 deletions examples/sourcepipeline/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ data "ncloud_sourcedeploy_project_stage_scenarios" "test-sourcedeploy_scenarios"
stage_id = data.ncloud_sourcedeploy_project_stages.test-sourcedeploy_stages.stages[0].id
}

data "ncloud_sourcepipeline_projects" "test-sourcepipeline" {
}

resource "ncloud_sourcepipeline_project" "test-sourcepipeline" {
name = "tf-sourcepipeline_project-test"
task {
Expand All @@ -47,9 +50,19 @@ resource "ncloud_sourcepipeline_project" "test-sourcepipeline" {
linked_tasks = ["task_name_1"]
}
triggers {
sourcecommit {
repository_name = ncloud_sourcecommit_repository.test-sourcecommit.name
branch = "master"
repository {
type = "sourcecommit"
name = ncloud_sourcecommit_repository.test-sourcecommit.name
branch = "master"
}
schedule {
day = ["MON", "TUE"]
time = "13:01"
timezone = "Asia/Seoul (UTC+09:00)"
execute_only_with_change = false
}
sourcepipeline {
id = data.ncloud_sourcepipeline_projects.test-sourcepipeline.projects[0].id
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/terraform-providers/terraform-provider-ncloud
go 1.16

require (
github.com/NaverCloudPlatform/ncloud-sdk-go-v2 v1.5.5
github.com/NaverCloudPlatform/ncloud-sdk-go-v2 v1.5.6
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-sdk/v2 v2.13.0
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
Expand Down
51 changes: 49 additions & 2 deletions ncloud/data_source_ncloud_sourcepipeline_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,16 @@ func dataSourceNcloudSourcePipelineProject() *schema.Resource {
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"sourcecommit": {
"repository": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"repository_name": {
"type": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
Expand All @@ -128,6 +132,49 @@ func dataSourceNcloudSourcePipelineProject() *schema.Resource {
},
},
},
"schedule": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"day": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"time": {
Type: schema.TypeString,
Computed: true,
},
"timezone": {
Type: schema.TypeString,
Computed: true,
},
"execute_only_with_change": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
"sourcepipeline": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
Expand Down
10 changes: 6 additions & 4 deletions ncloud/data_source_ncloud_sourcepipeline_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,12 @@ resource "ncloud_sourcepipeline_project" "test-project" {
linked_tasks = []
}
triggers {
sourcecommit {
repository_name = ncloud_sourcecommit_repository.test-repo.name
branch = "master"
}
schedule {
day = ["MON", "TUE"]
time = "13:01"
timezone = "Asia/Seoul (UTC+09:00)"
execute_only_with_change = false
}
}
}
Expand Down
79 changes: 79 additions & 0 deletions ncloud/data_source_ncloud_sourcepipeline_trigger_timezone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package ncloud

import (
"context"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func init() {
RegisterDataSource("ncloud_sourcepipeline_trigger_timezone", dataSourceNcloudSourcePipelineTimeZone())
}

func dataSourceNcloudSourcePipelineTimeZone() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceNcloudSourcePipelineTimeZoneRead,
Schema: map[string]*schema.Schema{
"output_file": {
Type: schema.TypeString,
Optional: true,
},
"timezone": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func dataSourceNcloudSourcePipelineTimeZoneRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*ProviderConfig)

timeZone, err := getSourcePipelineTimeZone(ctx, config)
if err != nil {
logErrorResponse("getSourcePipelineTimeZone", err, timeZone)
return diag.FromErr(err)
}
logResponse("getSourcePipelineTimeZone", timeZone)

if timeZone == nil {
d.SetId("")
return nil
}

d.SetId(time.Now().UTC().String())
d.Set("timezone", timeZone)

if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
return diag.FromErr(writeToFile(output.(string), timeZone))
}
return nil
}

func getSourcePipelineTimeZone(ctx context.Context, config *ProviderConfig) ([]*string, error) {
if config.SupportVPC {
return getVpcSourcePipelineTimeZone(ctx, config)
}
return getClassicSourcePipelineTimeZone(ctx, config)
}

func getClassicSourcePipelineTimeZone(ctx context.Context, config *ProviderConfig) ([]*string, error) {
resp, err := config.Client.sourcepipeline.V1Api.GetTimeZone(ctx)
if err != nil {
return nil, err
}
return resp.TimeZone, nil
}

func getVpcSourcePipelineTimeZone(ctx context.Context, config *ProviderConfig) ([]*string, error) {
resp, err := config.Client.vsourcepipeline.V1Api.GetTimeZone(ctx)
if err != nil {
return nil, err
}
return resp.TimeZone, nil
}
44 changes: 44 additions & 0 deletions ncloud/data_source_ncloud_sourcepipeline_trigger_timezone_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ncloud

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceNcloudSourcePipelineTriggerTimeZone_classic_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccClassicProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceNcloudSourcePipelineTriggerTimeZoneConfig(),
Check: resource.ComposeTestCheckFunc(
testAccCheckDataSourceID("data.ncloud_sourcepipeline_trigger_timezone.time_zone"),
),
},
},
})
}
func TestAccDataSourceNcloudSourcePipelineTriggerTimeZone_vpc_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceNcloudSourcePipelineTriggerTimeZoneConfig(),
Check: resource.ComposeTestCheckFunc(
testAccCheckDataSourceID("data.ncloud_sourcepipeline_trigger_timezone.time_zone"),
),
},
},
})
}

func testAccDataSourceNcloudSourcePipelineTriggerTimeZoneConfig() string {
return fmt.Sprintf(`
data "ncloud_sourcepipeline_trigger_timezone" "time_zone" {
}
`)
}
Loading

0 comments on commit cc301ea

Please sign in to comment.