Skip to content

Commit

Permalink
allow for resource import
Browse files Browse the repository at this point in the history
  • Loading branch information
jgramoll committed Aug 2, 2019
1 parent bd0c1d3 commit 76854a7
Show file tree
Hide file tree
Showing 47 changed files with 601 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,5 @@ resource "jenkins_job_datadog_job_property" "main" {

## TODO

1. import resources
1. importer project
1. Fragile TestAccJobBuildDiscarderPropertyStrategyLogRotatorBasic test
4 changes: 4 additions & 0 deletions client/cps_scm_flow_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ func NewCpsScmFlowDefinition() *CpsScmFlowDefinition {
Class: "org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition",
}
}

func (d *CpsScmFlowDefinition) GetId() string {
return d.Id
}
4 changes: 3 additions & 1 deletion client/job_build_discarder_property_strategy.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package client

type JobBuildDiscarderPropertyStrategy interface{}
type JobBuildDiscarderPropertyStrategy interface {
GetId() string
}
4 changes: 4 additions & 0 deletions client/job_build_discarder_property_strategy_log_rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ func NewJobBuildDiscarderPropertyStrategyLogRotator() *JobBuildDiscarderProperty
ArtifactNumToKeep: -1,
}
}

func (s *JobBuildDiscarderPropertyStrategyLogRotator) GetId() string {
return s.Id
}
4 changes: 3 additions & 1 deletion client/job_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ type JobDefinitionXml struct {
Item JobDefinition
}

type JobDefinition interface{}
type JobDefinition interface {
GetId() string
}

func (jobDefinition *JobDefinitionXml) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(jobDefinition.Item, start)
Expand Down
2 changes: 1 addition & 1 deletion client/job_gerrit_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type JobGerritTrigger struct {

Spec string `xml:"spec"`
Projects *JobGerritTriggerProjects `xml:"gerritProjects"`
dynamicGerritProjects *DynamicGerritProjects `xml:"dynamicGerritProjects"`
DynamicGerritProjects *DynamicGerritProjects `xml:"dynamicGerritProjects"`
SkipVote *JobGerritTriggerSkipVote `xml:"skipVote"`
SilentMode bool `xml:"silentMode"`
// notificationLevel
Expand Down
14 changes: 13 additions & 1 deletion provider/job_action_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// ErrInvalidJobActionId
var ErrInvalidJobActionId = errors.New("Invalid action id, must be jobName\xactionId")
var ErrInvalidJobActionId = errors.New("Invalid action id, must be jobName\\xactionId")

func resourceJobActionId(input string) (jobName string, actionId string, err error) {
parts := strings.Split(input, IdDelimiter)
Expand All @@ -24,6 +24,18 @@ func resourceJobActionId(input string) (jobName string, actionId string, err err
return
}

func resourceJobActionImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
jobName, _, err := resourceJobActionId(d.Id())
if err != nil {
return nil, err
}
err = d.Set("job", jobName)
if err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}

func resourceJobActionCreate(d *schema.ResourceData, m interface{}, createJobAction func() jobAction) error {
jobName := d.Get("job").(string)

Expand Down
3 changes: 3 additions & 0 deletions provider/job_build_discarder_property_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func jobBuildDiscarderPropertyResource() *schema.Resource {
Delete: func(d *schema.ResourceData, m interface{}) error {
return resourceJobPropertyDelete(d, m, newPropertyInterface)
},
Importer: &schema.ResourceImporter{
State: resourceJobPropertyImporter,
},

Schema: map[string]*schema.Schema{
"job": &schema.Schema{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ func jobBuildDiscarderPropertyStrategyLogRotatorResource() *schema.Resource {
}
return &schema.Resource{
Create: func(d *schema.ResourceData, m interface{}) error {
return resourceJobBuildDiscarderPropertyStrategyCreate(d, m, newJobBuildDiscarderPropertyStrategyLogRotatorInterface)
return resourceJobPropertyStrategyCreate(d, m, newJobBuildDiscarderPropertyStrategyLogRotatorInterface)
},
Update: func(d *schema.ResourceData, m interface{}) error {
return resourceJobBuildDiscarderPropertyStrategyUpdate(d, m, newJobBuildDiscarderPropertyStrategyLogRotatorInterface)
return resourceJobPropertyStrategyUpdate(d, m, newJobBuildDiscarderPropertyStrategyLogRotatorInterface)
},
Read: func(d *schema.ResourceData, m interface{}) error {
return resourceJobBuildDiscarderPropertyStrategyRead(d, m, newJobBuildDiscarderPropertyStrategyLogRotatorInterface)
return resourceJobPropertyStrategyRead(d, m, newJobBuildDiscarderPropertyStrategyLogRotatorInterface)
},
Delete: func(d *schema.ResourceData, m interface{}) error {
return resourceJobBuildDiscarderPropertyStrategyDelete(d, m, newJobBuildDiscarderPropertyStrategyLogRotatorInterface)
return resourceJobPropertyStrategyDelete(d, m, newJobBuildDiscarderPropertyStrategyLogRotatorInterface)
},
Importer: &schema.ResourceImporter{
State: resourceJobPropertyStrategyImporter,
},

Schema: map[string]*schema.Schema{
Expand Down
20 changes: 20 additions & 0 deletions provider/job_build_discarder_property_strategy_log_rotator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package provider

import (
"fmt"
"regexp"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
Expand Down Expand Up @@ -43,6 +45,24 @@ func TestAccJobBuildDiscarderPropertyStrategyLogRotatorBasic(t *testing.T) {
}, &strategyRefs, ensureJobBuildDiscarderPropertyStrategyLogRotator),
),
},
{
ResourceName: strategyResourceName,
ImportStateId: "invalid",
ImportState: true,
ExpectError: regexp.MustCompile("Invalid property strategy id"),
},
{
ResourceName: strategyResourceName,
ImportState: true,
ImportStateIdFunc: func(*terraform.State) (string, error) {
if len(strategyRefs) == 0 {
return "", fmt.Errorf("no strategies to import")
}
propertyId := (*jobRef.Properties.Items)[0].GetId()
return strings.Join([]string{jobName, propertyId, strategyRefs[0].GetId()}, IdDelimiter), nil
},
ImportStateVerify: true,
},
{
Config: testAccJobBuildDiscarderPropertyConfigBasic(jobName, 1),
Check: resource.ComposeAggregateTestCheckFunc(
Expand Down
31 changes: 23 additions & 8 deletions provider/job_build_discarder_property_strategy_resource.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package provider

import (
"errors"
"log"
"strings"

Expand All @@ -10,10 +11,13 @@ import (
"github.com/mitchellh/mapstructure"
)

// ErrInvalidPropertyId
var ErrInvalidPropertyStrategyId = errors.New("Invalid property strategy id, must be jobName_propertyId_strategyId")

func resourceJobPropertyStrategyId(input string) (jobName string, propertyId string, strategyId string, err error) {
parts := strings.Split(input, IdDelimiter)
if len(parts) != 3 {
err = ErrInvalidPropertyId
err = ErrInvalidPropertyStrategyId
return
}
jobName = parts[0]
Expand All @@ -22,7 +26,19 @@ func resourceJobPropertyStrategyId(input string) (jobName string, propertyId str
return
}

func resourceJobBuildDiscarderPropertyStrategyCreate(d *schema.ResourceData, m interface{}, createJobBuildDiscarderPropertyStrategy func() jobBuildDiscarderPropertyStrategy) error {
func resourceJobPropertyStrategyImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
jobName, propertyId, _, err := resourceJobPropertyStrategyId(d.Id())
if err != nil {
return nil, err
}
err = d.Set("property", strings.Join([]string{jobName, propertyId}, IdDelimiter))
if err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}

func resourceJobPropertyStrategyCreate(d *schema.ResourceData, m interface{}, createJobBuildDiscarderPropertyStrategy func() jobBuildDiscarderPropertyStrategy) error {
jobName, propertyId, err := resourceJobPropertyId(d.Get("property").(string))
if err != nil {
return err
Expand Down Expand Up @@ -63,10 +79,10 @@ func resourceJobBuildDiscarderPropertyStrategyCreate(d *schema.ResourceData, m i

d.SetId(strings.Join([]string{jobName, propertyId, strategyId}, IdDelimiter))
log.Println("[DEBUG] Creating build discarder propety strategy", d.Id())
return resourceJobBuildDiscarderPropertyStrategyRead(d, m, createJobBuildDiscarderPropertyStrategy)
return resourceJobPropertyStrategyRead(d, m, createJobBuildDiscarderPropertyStrategy)
}

func resourceJobBuildDiscarderPropertyStrategyRead(d *schema.ResourceData, m interface{}, createJobBuildDiscarderPropertyStrategy func() jobBuildDiscarderPropertyStrategy) error {
func resourceJobPropertyStrategyRead(d *schema.ResourceData, m interface{}, createJobBuildDiscarderPropertyStrategy func() jobBuildDiscarderPropertyStrategy) error {
jobService := m.(*Services).JobService
jobName, propertyId, err := resourceJobPropertyId(d.Get("property").(string))
if err != nil {
Expand Down Expand Up @@ -99,7 +115,7 @@ func resourceJobBuildDiscarderPropertyStrategyRead(d *schema.ResourceData, m int
return strategy.setResourceData(d)
}

func resourceJobBuildDiscarderPropertyStrategyUpdate(d *schema.ResourceData, m interface{}, createJobBuildDiscarderPropertyStrategy func() jobBuildDiscarderPropertyStrategy) error {
func resourceJobPropertyStrategyUpdate(d *schema.ResourceData, m interface{}, createJobBuildDiscarderPropertyStrategy func() jobBuildDiscarderPropertyStrategy) error {
jobName, propertyId, strategyId, err := resourceJobPropertyStrategyId(d.Id())
if err != nil {
return err
Expand Down Expand Up @@ -134,11 +150,10 @@ func resourceJobBuildDiscarderPropertyStrategyUpdate(d *schema.ResourceData, m i
}

log.Println("[DEBUG] Updating build discarder propety strategy", d.Id())
return resourceJobBuildDiscarderPropertyStrategyRead(d, m, createJobBuildDiscarderPropertyStrategy)
return resourceJobPropertyStrategyRead(d, m, createJobBuildDiscarderPropertyStrategy)
}

func resourceJobBuildDiscarderPropertyStrategyDelete(d *schema.ResourceData, m interface{}, createJobBuildDiscarderPropertyStrategy func() jobBuildDiscarderPropertyStrategy) error {

func resourceJobPropertyStrategyDelete(d *schema.ResourceData, m interface{}, createJobBuildDiscarderPropertyStrategy func() jobBuildDiscarderPropertyStrategy) error {
jobName, propertyId, err := resourceJobPropertyId(d.Get("property").(string))
if err != nil {
return err
Expand Down
30 changes: 30 additions & 0 deletions provider/job_build_discarder_property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package provider

import (
"fmt"
"regexp"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
Expand Down Expand Up @@ -33,6 +35,34 @@ func TestAccJobBuildDiscarderPropertyBasic(t *testing.T) {
}, &properties, ensureJobBuildDiscarderProperty),
),
},
{
ResourceName: property1,
ImportStateId: "invalid",
ImportState: true,
ExpectError: regexp.MustCompile("Invalid property id"),
},
{
ResourceName: property1,
ImportState: true,
ImportStateIdFunc: func(*terraform.State) (string, error) {
if len(properties) == 0 {
return "", fmt.Errorf("no properties to import")
}
return strings.Join([]string{jobName, properties[0].GetId()}, IdDelimiter), nil
},
ImportStateVerify: true,
},
{
ResourceName: property2,
ImportState: true,
ImportStateIdFunc: func(*terraform.State) (string, error) {
if len(properties) == 0 {
return "", fmt.Errorf("no properties to import")
}
return strings.Join([]string{jobName, properties[1].GetId()}, IdDelimiter), nil
},
ImportStateVerify: true,
},
{
Config: testAccJobBuildDiscarderPropertyConfigBasic(jobName, 1),
Check: resource.ComposeAggregateTestCheckFunc(
Expand Down
3 changes: 3 additions & 0 deletions provider/job_datadog_job_property_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func jobDatadogJobPropertyResource() *schema.Resource {
Delete: func(d *schema.ResourceData, m interface{}) error {
return resourceJobPropertyDelete(d, m, newPropertyInterface)
},
Importer: &schema.ResourceImporter{
State: resourceJobPropertyImporter,
},

Schema: map[string]*schema.Schema{
"job": &schema.Schema{
Expand Down
30 changes: 30 additions & 0 deletions provider/job_datadog_job_property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package provider

import (
"fmt"
"regexp"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
Expand Down Expand Up @@ -32,6 +34,34 @@ func TestAccJobDatadogJobPropertyBasic(t *testing.T) {
}, &properties, ensureJobDatadogJobProperty),
),
},
{
ResourceName: property1,
ImportStateId: "invalid",
ImportState: true,
ExpectError: regexp.MustCompile("Invalid property id"),
},
{
ResourceName: property1,
ImportState: true,
ImportStateIdFunc: func(*terraform.State) (string, error) {
if len(properties) == 0 {
return "", fmt.Errorf("no properties to import")
}
return strings.Join([]string{jobName, properties[0].GetId()}, IdDelimiter), nil
},
ImportStateVerify: true,
},
{
ResourceName: property2,
ImportState: true,
ImportStateIdFunc: func(*terraform.State) (string, error) {
if len(properties) == 0 {
return "", fmt.Errorf("no properties to import")
}
return strings.Join([]string{jobName, properties[0].GetId()}, IdDelimiter), nil
},
ImportStateVerify: true,
},
{
Config: testAccJobDatadogJobPropertyConfigBasic(jobName, 1),
Check: resource.ComposeAggregateTestCheckFunc(
Expand Down
3 changes: 3 additions & 0 deletions provider/job_declarative_job_action_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func jobDeclarativeJobActionResource() *schema.Resource {
Delete: func(d *schema.ResourceData, m interface{}) error {
return resourceJobActionDelete(d, m, newJobActionInterface)
},
Importer: &schema.ResourceImporter{
State: resourceJobActionImporter,
},

Schema: map[string]*schema.Schema{
"job": &schema.Schema{
Expand Down
19 changes: 19 additions & 0 deletions provider/job_declarative_job_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package provider

import (
"fmt"
"regexp"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
Expand Down Expand Up @@ -30,6 +32,23 @@ func TestAccJobDeclarativeJobActionBasic(t *testing.T) {
}, &actions, ensureJobDeclarativeJobAction),
),
},
{
ResourceName: actionResourceName,
ImportStateId: "invalid",
ImportState: true,
ExpectError: regexp.MustCompile("Invalid action id"),
},
{
ResourceName: actionResourceName,
ImportState: true,
ImportStateIdFunc: func(*terraform.State) (string, error) {
if len(actions) == 0 {
return "", fmt.Errorf("no actions to import")
}
return strings.Join([]string{jobName, actions[0].GetId()}, IdDelimiter), nil
},
ImportStateVerify: true,
},
{
Config: testAccJobConfigBasic(jobName),
Check: resource.ComposeAggregateTestCheckFunc(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func jobDeclarativeJobPropertyTrackerActionResource() *schema.Resource {
Delete: func(d *schema.ResourceData, m interface{}) error {
return resourceJobActionDelete(d, m, newJobActionInterface)
},
Importer: &schema.ResourceImporter{
State: resourceJobActionImporter,
},

Schema: map[string]*schema.Schema{
"job": &schema.Schema{
Expand Down
Loading

0 comments on commit 76854a7

Please sign in to comment.