Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
ilia-medvedev-codefresh committed Feb 26, 2024
2 parents a5d8706 + c650c77 commit 1321598
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 53 deletions.
39 changes: 20 additions & 19 deletions codefresh/cfclient/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,26 @@ func (t *CronTrigger) SetVariables(variables map[string]interface{}) {
}

type Spec struct {
Variables []Variable `json:"variables,omitempty"`
SpecTemplate *SpecTemplate `json:"specTemplate,omitempty"`
Triggers []Trigger `json:"triggers,omitempty"`
CronTriggers []CronTrigger `json:"cronTriggers,omitempty"`
Priority int `json:"priority,omitempty"`
Concurrency int `json:"concurrency,omitempty"`
BranchConcurrency int `json:"branchConcurrency,omitempty"`
TriggerConcurrency int `json:"triggerConcurrency,omitempty"`
Contexts []interface{} `json:"contexts,omitempty"`
Steps *Steps `json:"steps,omitempty"`
Stages *Stages `json:"stages,omitempty"`
Mode string `json:"mode,omitempty"`
FailFast *bool `json:"fail_fast,omitempty"`
RuntimeEnvironment RuntimeEnvironment `json:"runtimeEnvironment,omitempty"`
TerminationPolicy []map[string]interface{} `json:"terminationPolicy,omitempty"`
PackId string `json:"packId,omitempty"`
RequiredAvailableStorage string `json:"requiredAvailableStorage,omitempty"`
Hooks *Hooks `json:"hooks,omitempty"`
Options map[string]bool `json:"options,omitempty"`
Variables []Variable `json:"variables,omitempty"`
SpecTemplate *SpecTemplate `json:"specTemplate,omitempty"`
Triggers []Trigger `json:"triggers,omitempty"`
CronTriggers []CronTrigger `json:"cronTriggers,omitempty"`
Priority int `json:"priority,omitempty"`
Concurrency int `json:"concurrency,omitempty"`
BranchConcurrency int `json:"branchConcurrency,omitempty"`
TriggerConcurrency int `json:"triggerConcurrency,omitempty"`
Contexts []interface{} `json:"contexts,omitempty"`
Steps *Steps `json:"steps,omitempty"`
Stages *Stages `json:"stages,omitempty"`
Mode string `json:"mode,omitempty"`
FailFast *bool `json:"fail_fast,omitempty"`
RuntimeEnvironment RuntimeEnvironment `json:"runtimeEnvironment,omitempty"`
TerminationPolicy []map[string]interface{} `json:"terminationPolicy,omitempty"`
PackId string `json:"packId,omitempty"`
RequiredAvailableStorage string `json:"requiredAvailableStorage,omitempty"`
Hooks *Hooks `json:"hooks,omitempty"`
Options map[string]bool `json:"options,omitempty"`
PermitRestartFromFailedSteps bool `json:"permitRestartFromFailedSteps,omitempty"`
}

type Steps struct {
Expand Down
69 changes: 69 additions & 0 deletions codefresh/data_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package codefresh

import (
"fmt"

cfClient "github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceProject() *schema.Resource {
return &schema.Resource{
Description: "This data source retrieves a project by its ID or name.",
Read: dataSourceProjectRead,
Schema: map[string]*schema.Schema{
"_id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"tags": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func dataSourceProjectRead(d *schema.ResourceData, meta interface{}) error {

client := meta.(*cfClient.Client)
var project *cfClient.Project
var err error

if _id, _idOk := d.GetOk("_id"); _idOk {
project, err = client.GetProjectByID(_id.(string))
} else if name, nameOk := d.GetOk("name"); nameOk {
project, err = client.GetProjectByName(name.(string))
}

if err != nil {
return err
}

if project == nil {
return fmt.Errorf("data.codefresh_project - cannot find project")
}

return mapDataProjectToResource(project, d)

}

func mapDataProjectToResource(project *cfClient.Project, d *schema.ResourceData) error {

if project == nil || project.ID == "" {
return fmt.Errorf("data.codefresh_project - failed to mapDataProjectToResource")
}
d.SetId(project.ID)

d.Set("_id", project.ID)
d.Set("tags", project.Tags)

return nil
}
1 change: 1 addition & 0 deletions codefresh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func Provider() *schema.Provider {
"codefresh_registry": dataSourceRegistry(),
"codefresh_pipelines": dataSourcePipelines(),
"codefresh_account_idp": dataSourceAccountIdp(),
"codefresh_project": dataSourceProject(),
},
ResourcesMap: map[string]*schema.Resource{
"codefresh_account": resourceAccount(),
Expand Down
26 changes: 3 additions & 23 deletions codefresh/resource_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package codefresh
import (
"errors"
"fmt"
"strings"

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

client.Token = resp

apiKeys, err := client.GetApiKeysList()
if err != nil {
return nil
}

var keyID string
for _, key := range apiKeys {
if key.Name == apiKey.Name {
keyID = key.ID
}
}

if keyID == "" {
return errors.New("[ERROR] Key ID is not found.")
}

d.SetId(keyID)
// Codefresh tokens are in the form xxxxxxxxxxxx.xxxxxxxxx the first half serves as the id
d.SetId(strings.Split(resp, ".")[0])

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

client.Token = token

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

client.Token = token

err := client.UpdateAPIKey(&apiKey)
if err != nil {
return err
Expand Down
20 changes: 14 additions & 6 deletions codefresh/resource_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ Or: <code>original_yaml_string = file("/path/to/my/codefresh.yml")</code>
Optional: true,
Default: 0,
},
"permit_restart_from_failed_steps": {
Description: "Defines whether it is permitted to restart builds in this pipeline from failed step. Defaults to true",
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"spec_template": {
Description: "The pipeline's spec template.",
Type: schema.TypeList,
Expand Down Expand Up @@ -774,6 +780,7 @@ func flattenSpec(spec cfclient.Spec) []interface{} {
m["concurrency"] = spec.Concurrency
m["branch_concurrency"] = spec.BranchConcurrency
m["trigger_concurrency"] = spec.TriggerConcurrency
m["permit_restart_from_failed_steps"] = spec.PermitRestartFromFailedSteps

m["priority"] = spec.Priority

Expand Down Expand Up @@ -923,12 +930,13 @@ func mapResourceToPipeline(d *schema.ResourceData) (*cfclient.Pipeline, error) {
OriginalYamlString: originalYamlString,
},
Spec: cfclient.Spec{
PackId: d.Get("spec.0.pack_id").(string),
RequiredAvailableStorage: d.Get("spec.0.required_available_storage").(string),
Priority: d.Get("spec.0.priority").(int),
Concurrency: d.Get("spec.0.concurrency").(int),
BranchConcurrency: d.Get("spec.0.branch_concurrency").(int),
TriggerConcurrency: d.Get("spec.0.trigger_concurrency").(int),
PackId: d.Get("spec.0.pack_id").(string),
RequiredAvailableStorage: d.Get("spec.0.required_available_storage").(string),
Priority: d.Get("spec.0.priority").(int),
Concurrency: d.Get("spec.0.concurrency").(int),
BranchConcurrency: d.Get("spec.0.branch_concurrency").(int),
TriggerConcurrency: d.Get("spec.0.trigger_concurrency").(int),
PermitRestartFromFailedSteps: d.Get("spec.0.permit_restart_from_failed_steps").(bool),
},
}

Expand Down
60 changes: 60 additions & 0 deletions codefresh/resource_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,39 @@ func TestAccCodefreshPipeline_Concurrency(t *testing.T) {
})
}

func TestAccCodefreshPipeline_PremitRestartFromFailedSteps(t *testing.T) {
name := pipelineNamePrefix + acctest.RandString(10)
resourceName := "codefresh_pipeline.test"
var pipeline cfclient.Pipeline

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCodefreshPipelineDestroy,
Steps: []resource.TestStep{
{
Config: testAccCodefreshPipelineBasicConfigPermitRestartFromFailedSteps(name, "codefresh-contrib/react-sample-app", "./codefresh.yml", "master", "git", true),
Check: resource.ComposeTestCheckFunc(
testAccCheckCodefreshPipelineExists(resourceName, &pipeline),
resource.TestCheckResourceAttr(resourceName, "spec.0.permit_restart_from_failed_steps", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccCodefreshPipelineBasicConfigPermitRestartFromFailedSteps(name, "codefresh-contrib/react-sample-app", "./codefresh.yml", "master", "git", false),
Check: resource.ComposeTestCheckFunc(
testAccCheckCodefreshPipelineExists(resourceName, &pipeline),
resource.TestCheckResourceAttr(resourceName, "spec.0.permit_restart_from_failed_steps", "false"),
),
},
},
})
}

func TestAccCodefreshPipeline_Tags(t *testing.T) {
name := pipelineNamePrefix + acctest.RandString(10)
resourceName := "codefresh_pipeline.test"
Expand Down Expand Up @@ -955,6 +988,33 @@ resource "codefresh_pipeline" "test" {
`, rName, repo, path, revision, context, concurrency, concurrencyBranch, concurrencyTrigger)
}

func testAccCodefreshPipelineBasicConfigPermitRestartFromFailedSteps(rName string, repo string, path string, revision string, context string, permitRestartFromFailedSteps bool) string {
return fmt.Sprintf(`
resource "codefresh_pipeline" "test" {
lifecycle {
ignore_changes = [
revision
]
}
name = "%s"
spec {
spec_template {
repo = %q
path = %q
revision = %q
context = %q
}
permit_restart_from_failed_steps = %t
}
}
`, rName, repo, path, revision, context, permitRestartFromFailedSteps)
}

func testAccCodefreshPipelineBasicConfigTriggers(
rName,
repo,
Expand Down
40 changes: 40 additions & 0 deletions docs/data-sources/project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
page_title: "codefresh_project Data Source - terraform-provider-codefresh"
subcategory: ""
description: |-
This data source retrieves a project by its ID or name.
---

# codefresh_project (Data Source)

This data source retrieves a project by its ID or name.

## Example Usage

```hcl
data "codefresh_project" "myapp" {
name = "myapp"
}
resource "codefresh_pipeline" "myapp-deploy" {
name = "${data.codefresh_project.myapp.projectName}/myapp-deploy"
...
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `_id` (String)
- `name` (String)
- `tags` (List of String)

### Read-Only

- `id` (String) The ID of this resource.
4 changes: 2 additions & 2 deletions docs/resources/api_key.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ description: |-

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

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.

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.
This resource requires Codefresh system admin permissions, hence is relevant for on-prem deployments of Codefresh only.


## Example usage
Expand Down
1 change: 1 addition & 0 deletions docs/resources/pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Optional:
- `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))
- `options` (Block List, Max: 1) The options for the pipeline. (see [below for nested schema](#nestedblock--spec--options))
- `pack_id` (String) SAAS pack (`5cd1746617313f468d669013` for Small; `5cd1746717313f468d669014` for Medium; `5cd1746817313f468d669015` for Large; `5cd1746817313f468d669017` for XL; `5cd1746817313f468d669018` for XXL); `5cd1746817313f468d669020` for 4XL).
- `permit_restart_from_failed_steps` (Boolean) Defines whether it is permitted to restart builds in this pipeline from failed step. Defaults to true
- `priority` (Number) Helps to organize the order of builds execution in case of reaching the concurrency limit (default: `0`).
- `required_available_storage` (String) Minimum disk space required for build filesystem ( unit Gi is required).
- `runtime_environment` (Block List) The runtime environment for the pipeline. (see [below for nested schema](#nestedblock--spec--runtime_environment))
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
providerAddr = codefresh.DEFAULT_CODEFRESH_PLUGIN_ADDR
}
plugin.Serve(&plugin.ServeOpts{
ProviderAddr: providerAddr, // Required for debug attaching
ProviderAddr: providerAddr,
ProviderFunc: codefresh.Provider,
Debug: debugMode,
})
Expand Down
29 changes: 29 additions & 0 deletions templates/data-sources/project.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
page_title: "{{.Name}} {{.Type}} - {{.ProviderName}}"
subcategory: ""
description: |-
{{ .Description | plainmarkdown | trimspace | prefixlines " " }}
---

# {{.Name}} ({{.Type}})

{{ .Description | trimspace }}

## Example Usage

```hcl
data "codefresh_project" "myapp" {
name = "myapp"
}


resource "codefresh_pipeline" "myapp-deploy" {

name = "${data.codefresh_project.myapp.projectName}/myapp-deploy"

...
}

```

{{ .SchemaMarkdown | trimspace }}
Loading

0 comments on commit 1321598

Please sign in to comment.