Skip to content

Commit 1321598

Browse files
merge master
2 parents a5d8706 + c650c77 commit 1321598

File tree

12 files changed

+242
-53
lines changed

12 files changed

+242
-53
lines changed

codefresh/cfclient/pipeline.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -103,25 +103,26 @@ func (t *CronTrigger) SetVariables(variables map[string]interface{}) {
103103
}
104104

105105
type Spec struct {
106-
Variables []Variable `json:"variables,omitempty"`
107-
SpecTemplate *SpecTemplate `json:"specTemplate,omitempty"`
108-
Triggers []Trigger `json:"triggers,omitempty"`
109-
CronTriggers []CronTrigger `json:"cronTriggers,omitempty"`
110-
Priority int `json:"priority,omitempty"`
111-
Concurrency int `json:"concurrency,omitempty"`
112-
BranchConcurrency int `json:"branchConcurrency,omitempty"`
113-
TriggerConcurrency int `json:"triggerConcurrency,omitempty"`
114-
Contexts []interface{} `json:"contexts,omitempty"`
115-
Steps *Steps `json:"steps,omitempty"`
116-
Stages *Stages `json:"stages,omitempty"`
117-
Mode string `json:"mode,omitempty"`
118-
FailFast *bool `json:"fail_fast,omitempty"`
119-
RuntimeEnvironment RuntimeEnvironment `json:"runtimeEnvironment,omitempty"`
120-
TerminationPolicy []map[string]interface{} `json:"terminationPolicy,omitempty"`
121-
PackId string `json:"packId,omitempty"`
122-
RequiredAvailableStorage string `json:"requiredAvailableStorage,omitempty"`
123-
Hooks *Hooks `json:"hooks,omitempty"`
124-
Options map[string]bool `json:"options,omitempty"`
106+
Variables []Variable `json:"variables,omitempty"`
107+
SpecTemplate *SpecTemplate `json:"specTemplate,omitempty"`
108+
Triggers []Trigger `json:"triggers,omitempty"`
109+
CronTriggers []CronTrigger `json:"cronTriggers,omitempty"`
110+
Priority int `json:"priority,omitempty"`
111+
Concurrency int `json:"concurrency,omitempty"`
112+
BranchConcurrency int `json:"branchConcurrency,omitempty"`
113+
TriggerConcurrency int `json:"triggerConcurrency,omitempty"`
114+
Contexts []interface{} `json:"contexts,omitempty"`
115+
Steps *Steps `json:"steps,omitempty"`
116+
Stages *Stages `json:"stages,omitempty"`
117+
Mode string `json:"mode,omitempty"`
118+
FailFast *bool `json:"fail_fast,omitempty"`
119+
RuntimeEnvironment RuntimeEnvironment `json:"runtimeEnvironment,omitempty"`
120+
TerminationPolicy []map[string]interface{} `json:"terminationPolicy,omitempty"`
121+
PackId string `json:"packId,omitempty"`
122+
RequiredAvailableStorage string `json:"requiredAvailableStorage,omitempty"`
123+
Hooks *Hooks `json:"hooks,omitempty"`
124+
Options map[string]bool `json:"options,omitempty"`
125+
PermitRestartFromFailedSteps bool `json:"permitRestartFromFailedSteps,omitempty"`
125126
}
126127

127128
type Steps struct {

codefresh/data_project.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package codefresh
2+
3+
import (
4+
"fmt"
5+
6+
cfClient "github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceProject() *schema.Resource {
11+
return &schema.Resource{
12+
Description: "This data source retrieves a project by its ID or name.",
13+
Read: dataSourceProjectRead,
14+
Schema: map[string]*schema.Schema{
15+
"_id": {
16+
Type: schema.TypeString,
17+
Optional: true,
18+
},
19+
"name": {
20+
Type: schema.TypeString,
21+
Optional: true,
22+
},
23+
"tags": {
24+
Type: schema.TypeList,
25+
Optional: true,
26+
Elem: &schema.Schema{
27+
Type: schema.TypeString,
28+
},
29+
},
30+
},
31+
}
32+
}
33+
34+
func dataSourceProjectRead(d *schema.ResourceData, meta interface{}) error {
35+
36+
client := meta.(*cfClient.Client)
37+
var project *cfClient.Project
38+
var err error
39+
40+
if _id, _idOk := d.GetOk("_id"); _idOk {
41+
project, err = client.GetProjectByID(_id.(string))
42+
} else if name, nameOk := d.GetOk("name"); nameOk {
43+
project, err = client.GetProjectByName(name.(string))
44+
}
45+
46+
if err != nil {
47+
return err
48+
}
49+
50+
if project == nil {
51+
return fmt.Errorf("data.codefresh_project - cannot find project")
52+
}
53+
54+
return mapDataProjectToResource(project, d)
55+
56+
}
57+
58+
func mapDataProjectToResource(project *cfClient.Project, d *schema.ResourceData) error {
59+
60+
if project == nil || project.ID == "" {
61+
return fmt.Errorf("data.codefresh_project - failed to mapDataProjectToResource")
62+
}
63+
d.SetId(project.ID)
64+
65+
d.Set("_id", project.ID)
66+
d.Set("tags", project.Tags)
67+
68+
return nil
69+
}

codefresh/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func Provider() *schema.Provider {
5252
"codefresh_registry": dataSourceRegistry(),
5353
"codefresh_pipelines": dataSourcePipelines(),
5454
"codefresh_account_idp": dataSourceAccountIdp(),
55+
"codefresh_project": dataSourceProject(),
5556
},
5657
ResourcesMap: map[string]*schema.Resource{
5758
"codefresh_account": resourceAccount(),

codefresh/resource_api_key.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package codefresh
33
import (
44
"errors"
55
"fmt"
6+
"strings"
67

78
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
89
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/internal/datautil"
@@ -93,25 +94,8 @@ func resourceApiKeyCreate(d *schema.ResourceData, meta interface{}) error {
9394
return err
9495
}
9596

96-
client.Token = resp
97-
98-
apiKeys, err := client.GetApiKeysList()
99-
if err != nil {
100-
return nil
101-
}
102-
103-
var keyID string
104-
for _, key := range apiKeys {
105-
if key.Name == apiKey.Name {
106-
keyID = key.ID
107-
}
108-
}
109-
110-
if keyID == "" {
111-
return errors.New("[ERROR] Key ID is not found.")
112-
}
113-
114-
d.SetId(keyID)
97+
// Codefresh tokens are in the form xxxxxxxxxxxx.xxxxxxxxx the first half serves as the id
98+
d.SetId(strings.Split(resp, ".")[0])
11599

116100
return nil
117101
}
@@ -132,8 +116,6 @@ func resourceApiKeyRead(d *schema.ResourceData, meta interface{}) error {
132116
return errors.New("[ERROR] Can't read API Key. Token is empty.")
133117
}
134118

135-
client.Token = token
136-
137119
apiKey, err := client.GetAPIKey(keyID)
138120
if err != nil {
139121
return err
@@ -157,8 +139,6 @@ func resourceApiKeyUpdate(d *schema.ResourceData, meta interface{}) error {
157139
return errors.New("[ERROR] Can't read API Key. Token is empty.")
158140
}
159141

160-
client.Token = token
161-
162142
err := client.UpdateAPIKey(&apiKey)
163143
if err != nil {
164144
return err

codefresh/resource_pipeline.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ Or: <code>original_yaml_string = file("/path/to/my/codefresh.yml")</code>
101101
Optional: true,
102102
Default: 0,
103103
},
104+
"permit_restart_from_failed_steps": {
105+
Description: "Defines whether it is permitted to restart builds in this pipeline from failed step. Defaults to true",
106+
Type: schema.TypeBool,
107+
Optional: true,
108+
Default: true,
109+
},
104110
"spec_template": {
105111
Description: "The pipeline's spec template.",
106112
Type: schema.TypeList,
@@ -774,6 +780,7 @@ func flattenSpec(spec cfclient.Spec) []interface{} {
774780
m["concurrency"] = spec.Concurrency
775781
m["branch_concurrency"] = spec.BranchConcurrency
776782
m["trigger_concurrency"] = spec.TriggerConcurrency
783+
m["permit_restart_from_failed_steps"] = spec.PermitRestartFromFailedSteps
777784

778785
m["priority"] = spec.Priority
779786

@@ -923,12 +930,13 @@ func mapResourceToPipeline(d *schema.ResourceData) (*cfclient.Pipeline, error) {
923930
OriginalYamlString: originalYamlString,
924931
},
925932
Spec: cfclient.Spec{
926-
PackId: d.Get("spec.0.pack_id").(string),
927-
RequiredAvailableStorage: d.Get("spec.0.required_available_storage").(string),
928-
Priority: d.Get("spec.0.priority").(int),
929-
Concurrency: d.Get("spec.0.concurrency").(int),
930-
BranchConcurrency: d.Get("spec.0.branch_concurrency").(int),
931-
TriggerConcurrency: d.Get("spec.0.trigger_concurrency").(int),
933+
PackId: d.Get("spec.0.pack_id").(string),
934+
RequiredAvailableStorage: d.Get("spec.0.required_available_storage").(string),
935+
Priority: d.Get("spec.0.priority").(int),
936+
Concurrency: d.Get("spec.0.concurrency").(int),
937+
BranchConcurrency: d.Get("spec.0.branch_concurrency").(int),
938+
TriggerConcurrency: d.Get("spec.0.trigger_concurrency").(int),
939+
PermitRestartFromFailedSteps: d.Get("spec.0.permit_restart_from_failed_steps").(bool),
932940
},
933941
}
934942

codefresh/resource_pipeline_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,39 @@ func TestAccCodefreshPipeline_Concurrency(t *testing.T) {
7979
})
8080
}
8181

82+
func TestAccCodefreshPipeline_PremitRestartFromFailedSteps(t *testing.T) {
83+
name := pipelineNamePrefix + acctest.RandString(10)
84+
resourceName := "codefresh_pipeline.test"
85+
var pipeline cfclient.Pipeline
86+
87+
resource.ParallelTest(t, resource.TestCase{
88+
PreCheck: func() { testAccPreCheck(t) },
89+
Providers: testAccProviders,
90+
CheckDestroy: testAccCheckCodefreshPipelineDestroy,
91+
Steps: []resource.TestStep{
92+
{
93+
Config: testAccCodefreshPipelineBasicConfigPermitRestartFromFailedSteps(name, "codefresh-contrib/react-sample-app", "./codefresh.yml", "master", "git", true),
94+
Check: resource.ComposeTestCheckFunc(
95+
testAccCheckCodefreshPipelineExists(resourceName, &pipeline),
96+
resource.TestCheckResourceAttr(resourceName, "spec.0.permit_restart_from_failed_steps", "true"),
97+
),
98+
},
99+
{
100+
ResourceName: resourceName,
101+
ImportState: true,
102+
ImportStateVerify: true,
103+
},
104+
{
105+
Config: testAccCodefreshPipelineBasicConfigPermitRestartFromFailedSteps(name, "codefresh-contrib/react-sample-app", "./codefresh.yml", "master", "git", false),
106+
Check: resource.ComposeTestCheckFunc(
107+
testAccCheckCodefreshPipelineExists(resourceName, &pipeline),
108+
resource.TestCheckResourceAttr(resourceName, "spec.0.permit_restart_from_failed_steps", "false"),
109+
),
110+
},
111+
},
112+
})
113+
}
114+
82115
func TestAccCodefreshPipeline_Tags(t *testing.T) {
83116
name := pipelineNamePrefix + acctest.RandString(10)
84117
resourceName := "codefresh_pipeline.test"
@@ -955,6 +988,33 @@ resource "codefresh_pipeline" "test" {
955988
`, rName, repo, path, revision, context, concurrency, concurrencyBranch, concurrencyTrigger)
956989
}
957990

991+
func testAccCodefreshPipelineBasicConfigPermitRestartFromFailedSteps(rName string, repo string, path string, revision string, context string, permitRestartFromFailedSteps bool) string {
992+
return fmt.Sprintf(`
993+
resource "codefresh_pipeline" "test" {
994+
995+
lifecycle {
996+
ignore_changes = [
997+
revision
998+
]
999+
}
1000+
1001+
name = "%s"
1002+
1003+
spec {
1004+
spec_template {
1005+
repo = %q
1006+
path = %q
1007+
revision = %q
1008+
context = %q
1009+
}
1010+
1011+
permit_restart_from_failed_steps = %t
1012+
1013+
}
1014+
}
1015+
`, rName, repo, path, revision, context, permitRestartFromFailedSteps)
1016+
}
1017+
9581018
func testAccCodefreshPipelineBasicConfigTriggers(
9591019
rName,
9601020
repo,

docs/data-sources/project.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
page_title: "codefresh_project Data Source - terraform-provider-codefresh"
3+
subcategory: ""
4+
description: |-
5+
This data source retrieves a project by its ID or name.
6+
---
7+
8+
# codefresh_project (Data Source)
9+
10+
This data source retrieves a project by its ID or name.
11+
12+
## Example Usage
13+
14+
```hcl
15+
data "codefresh_project" "myapp" {
16+
name = "myapp"
17+
}
18+
19+
20+
resource "codefresh_pipeline" "myapp-deploy" {
21+
22+
name = "${data.codefresh_project.myapp.projectName}/myapp-deploy"
23+
24+
...
25+
}
26+
27+
```
28+
29+
<!-- schema generated by tfplugindocs -->
30+
## Schema
31+
32+
### Optional
33+
34+
- `_id` (String)
35+
- `name` (String)
36+
- `tags` (List of String)
37+
38+
### Read-Only
39+
40+
- `id` (String) The ID of this resource.

docs/resources/api_key.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ description: |-
99

1010
Manages an API Key tied to an Account and a User.
1111

12-
terraform-provider-codefresh itself uses an API key, passed as provider's attribute, but it's possible to use that API Key to generate a new one.
13-
12+
terraform-provider-codefresh itself uses an API key, passed as provider's attribute, but it's possible to use that API Key to generate a new one.
13+
This resource requires Codefresh system admin permissions, hence is relevant for on-prem deployments of Codefresh only.
1414

1515

1616
## Example usage

docs/resources/pipeline.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ Optional:
130130
- `cron_trigger` (Block List) The pipeline's cron triggers. Conflicts with the deprecated [codefresh_pipeline_cron_trigger](https://registry.terraform.io/providers/codefresh-io/codefresh/latest/docs/resources/pipeline_cron_trigger) resource. (see [below for nested schema](#nestedblock--spec--cron_trigger))
131131
- `options` (Block List, Max: 1) The options for the pipeline. (see [below for nested schema](#nestedblock--spec--options))
132132
- `pack_id` (String) SAAS pack (`5cd1746617313f468d669013` for Small; `5cd1746717313f468d669014` for Medium; `5cd1746817313f468d669015` for Large; `5cd1746817313f468d669017` for XL; `5cd1746817313f468d669018` for XXL); `5cd1746817313f468d669020` for 4XL).
133+
- `permit_restart_from_failed_steps` (Boolean) Defines whether it is permitted to restart builds in this pipeline from failed step. Defaults to true
133134
- `priority` (Number) Helps to organize the order of builds execution in case of reaching the concurrency limit (default: `0`).
134135
- `required_available_storage` (String) Minimum disk space required for build filesystem ( unit Gi is required).
135136
- `runtime_environment` (Block List) The runtime environment for the pipeline. (see [below for nested schema](#nestedblock--spec--runtime_environment))

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func main() {
1717
providerAddr = codefresh.DEFAULT_CODEFRESH_PLUGIN_ADDR
1818
}
1919
plugin.Serve(&plugin.ServeOpts{
20-
ProviderAddr: providerAddr, // Required for debug attaching
20+
ProviderAddr: providerAddr,
2121
ProviderFunc: codefresh.Provider,
2222
Debug: debugMode,
2323
})

0 commit comments

Comments
 (0)