|
| 1 | +package codefresh |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "time" |
| 7 | + |
| 8 | + cfClient "github.com/codefresh-io/terraform-provider-codefresh/client" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +func dataSourcePipelines() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Description: "This resource retrives all pipelines belonging to the current user, which can be optionally filtered by the name.", |
| 15 | + Read: dataSourcePipelinesRead, |
| 16 | + Schema: map[string]*schema.Schema{ |
| 17 | + "name_regex": { |
| 18 | + Description: "The name regular expression to filter pipelines by.", |
| 19 | + Type: schema.TypeString, |
| 20 | + Optional: true, |
| 21 | + }, |
| 22 | + "pipelines": { |
| 23 | + Description: "The returned list of pipelines. Note that `spec` is currently limited to the YAML, because of the complexity of the object.", |
| 24 | + Type: schema.TypeList, |
| 25 | + Computed: true, |
| 26 | + Elem: &schema.Resource{ |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "id": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Computed: true, |
| 31 | + }, |
| 32 | + "name": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Computed: true, |
| 35 | + }, |
| 36 | + "project": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Computed: true, |
| 39 | + }, |
| 40 | + "tags": { |
| 41 | + Type: schema.TypeList, |
| 42 | + Computed: true, |
| 43 | + Elem: schema.TypeString, |
| 44 | + }, |
| 45 | + "is_public": { |
| 46 | + Type: schema.TypeBool, |
| 47 | + Computed: true, |
| 48 | + }, |
| 49 | + "spec": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Computed: true, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func dataSourcePipelinesRead(d *schema.ResourceData, meta interface{}) error { |
| 61 | + |
| 62 | + client := meta.(*cfClient.Client) |
| 63 | + |
| 64 | + pipelines, err := client.GetPipelines() |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + err = mapDataPipelinesToResource(*pipelines, d) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + d.SetId(time.Now().UTC().String()) |
| 75 | + |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func mapDataPipelinesToResource(pipelines []cfClient.Pipeline, d *schema.ResourceData) error { |
| 80 | + var res = make([]map[string]interface{}, len(pipelines)) |
| 81 | + for i, p := range pipelines { |
| 82 | + m := make(map[string]interface{}) |
| 83 | + m["id"] = p.Metadata.ID |
| 84 | + m["name"] = p.Metadata.Name |
| 85 | + m["project"] = p.Metadata.Project |
| 86 | + m["tags"] = p.Metadata.Labels.Tags |
| 87 | + m["is_public"] = p.Metadata.IsPublic |
| 88 | + m["spec"] = p.Metadata.OriginalYamlString |
| 89 | + |
| 90 | + res[i] = m |
| 91 | + } |
| 92 | + |
| 93 | + filteredPipelines := make([]map[string]interface{}, 0) |
| 94 | + for _, p := range res { |
| 95 | + match := false |
| 96 | + |
| 97 | + name, ok := d.GetOk("name_regex") |
| 98 | + if !ok { |
| 99 | + match = true |
| 100 | + } else { |
| 101 | + r, err := regexp.Compile(name.(string)) |
| 102 | + if err != nil { |
| 103 | + return fmt.Errorf("`name_regex` is not a valid regular expression, %s", err.Error()) |
| 104 | + } |
| 105 | + match = r.MatchString(p["name"].(string)) |
| 106 | + } |
| 107 | + |
| 108 | + if match { |
| 109 | + filteredPipelines = append(filteredPipelines, p) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + err := d.Set("pipelines", filteredPipelines) |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + return nil |
| 119 | +} |
0 commit comments