Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a provider for scheduled pipelines #81

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
32 changes: 30 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
version: 2
version: 2.1

orbs:
snyk: snyk/[email protected]

executors:
go:
docker:
- image: cimg/go:1.23

jobs:
build:
docker:
Expand Down Expand Up @@ -54,14 +63,33 @@ jobs:
command: |
ghr -t ${GITHUB_TOKEN} -u ${CIRCLE_PROJECT_USERNAME} -r ${CIRCLE_PROJECT_REPONAME} -c ${CIRCLE_SHA1} -delete -draft ${CIRCLE_TAG} .

vulnerability-scan:
executor: go
steps:
- checkout
- run:
name: Setup Scanning
command: |
git config --global url."https://$GITHUB_USER:[email protected]/circleci/".insteadOf "https://github.com/circleci/"
- run:
name: Launching Snyk Orb Scanning
command: echo "Running snyk/scan and displaying the results"
- snyk/scan:
organization: "circleci-public"
fail-on-issues: true
severity-threshold: high
monitor-on-build: false
additional-arguments: "--all-projects -d"

workflows:
version: 2
build:
jobs:
- build:
filters:
tags:
only: /^v\d+\.\d+\.\d+$/
- vulnerability-scan:
context: org-global-employees
- release:
requires:
- build
Expand Down
17 changes: 13 additions & 4 deletions circleci/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// It uses upstream client functionality where possible and defines its own methods as needed
type Client struct {
contexts *api.ContextRestClient
schedules *api.ScheduleRestClient
rest *rest.Client
vcs string
organization string
Expand All @@ -39,19 +40,27 @@ func New(config Config) (*Client, error) {

rootURL := fmt.Sprintf("%s://%s", u.Scheme, u.Host)

contexts, err := api.NewContextRestClient(settings.Config{
cfg := settings.Config{
Host: rootURL,
RestEndpoint: u.Path,
Token: config.Token,
HTTPClient: http.DefaultClient,
})
}

contexts, err := api.NewContextRestClient(cfg)
if err != nil {
return nil, err
}

schedules, err := api.NewScheduleRestClient(cfg)
if err != nil {
return nil, err
}

return &Client{
rest: rest.New(rootURL, u.Path, config.Token),
contexts: contexts,
rest: rest.New(rootURL, u.Path, config.Token),
contexts: contexts,
schedules: schedules,

vcs: config.VCS,
organization: config.Organization,
Expand Down
21 changes: 21 additions & 0 deletions circleci/client/schedule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package client

import (
"github.com/CircleCI-Public/circleci-cli/api"
)

func (c *Client) GetSchedule(id string) (*api.Schedule, error) {
return c.schedules.ScheduleByID(id)
}

func (c *Client) CreateSchedule(organization, project, name, description string, timetable api.Timetable, useSchedulingSystem bool, parameters map[string]string) (*api.Schedule, error) {
return c.schedules.CreateSchedule(c.vcs, organization, project, name, description, useSchedulingSystem, timetable, parameters)
}

func (c *Client) DeleteSchedule(id string) error {
return c.schedules.DeleteSchedule(id)
}

func (c *Client) UpdateSchedule(id, name, description string, timetable api.Timetable, useSchedulingActor bool, parameters map[string]string) (*api.Schedule, error) {
return c.schedules.UpdateSchedule(id, name, description, useSchedulingActor, timetable, parameters)
}
1 change: 1 addition & 0 deletions circleci/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func Provider() terraform.ResourceProvider {
"circleci_environment_variable": resourceCircleCIEnvironmentVariable(),
"circleci_context": resourceCircleCIContext(),
"circleci_context_environment_variable": resourceCircleCIContextEnvironmentVariable(),
"circleci_schedule": resourceCircleCISchedule(),
},
DataSourcesMap: map[string]*schema.Resource{
"circleci_context": dataSourceCircleCIContext(),
Expand Down
4 changes: 4 additions & 0 deletions circleci/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ func testAccPreCheck(t *testing.T) {
if v := os.Getenv("TEST_CIRCLECI_ORGANIZATION"); v == "" {
t.Fatal("TEST_CIRCLECI_ORGANIZATION must be set for acceptance tests")
}

if v := os.Getenv("CIRCLECI_PROJECT"); v == "" {
t.Fatal("CIRCLECI_PROJECT must be set for acceptance tests")
}
}
268 changes: 268 additions & 0 deletions circleci/resource_circleci_schedule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
package circleci

import (
"fmt"
"strings"

"github.com/CircleCI-Public/circleci-cli/api"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

client "github.com/mrolla/terraform-provider-circleci/circleci/client"
)

// NB Magic scheduled actor ID
const scheduledActorID = "d9b3fcaa-6032-405a-8c75-40079ce33c3e"

func resourceCircleCISchedule() *schema.Resource {
return &schema.Resource{
Create: resourceCircleCIScheduleCreate,
Read: resourceCircleCIScheduleRead,
Delete: resourceCircleCIScheduleDelete,
Update: resourceCircleCIScheduleUpdate,
Importer: &schema.ResourceImporter{
State: resourceCircleCIScheduleImport,
},
Schema: map[string]*schema.Schema{
"organization": {
Type: schema.TypeString,
Description: "The organization where the schedule will be created",
Optional: true,
ForceNew: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return old == d.Get("organization").(string)
},
},
"project": {
Type: schema.TypeString,
Description: "The name of the CircleCI project to create the schedule in",
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Description: "The name of the schedule",
Required: true,
},
"description": {
Type: schema.TypeString,
Description: "The description of the schedule",
Optional: true,
},
"per_hour": {
Type: schema.TypeInt,
Description: "How often per hour to trigger a pipeline",
Required: true,
},
"hours_of_day": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Description: "Which hours of the day to trigger a pipeline",
Required: true,
},
"days_of_week": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "Which days of the week (\"MON\" .. \"SUN\") to trigger a pipeline on",
Required: true,
},
"use_scheduling_system": {
Type: schema.TypeBool,
Description: "Use the scheduled system actor for attribution",
Required: true,
},
"parameters": {
Type: schema.TypeMap,
Description: "Pipeline parameters to pass to created pipelines",
Optional: true,
},
},
}
}

func resourceCircleCIScheduleCreate(d *schema.ResourceData, m interface{}) error {
c := m.(*client.Client)

organization, err := c.Organization(d.Get("organization").(string))
if err != nil {
return err
}

project := d.Get("project").(string)
name := d.Get("name").(string)
description := d.Get("description").(string)
useSchedulingSystem := d.Get("use_scheduling_system").(bool)

parsedHours := d.Get("hours_of_day").([]interface{})
var hoursOfDay []uint
for _, hour := range parsedHours {
hoursOfDay = append(hoursOfDay, uint(hour.(int)))
}

var exists = struct{}{}
validDays := make(map[string]interface{})
validDays["MON"] = exists
validDays["TUE"] = exists
validDays["WED"] = exists
validDays["THU"] = exists
validDays["FRI"] = exists
validDays["SAT"] = exists
validDays["SUN"] = exists

parsedDays := d.Get("days_of_week").([]interface{})
var daysOfWeek []string
for _, day := range parsedDays {
if validDays[day.(string)] == nil {
return fmt.Errorf("Invalid day specified: %s", day)
}
daysOfWeek = append(daysOfWeek, day.(string))
}

timetable := api.Timetable{
PerHour: uint(d.Get("per_hour").(int)),
HoursOfDay: hoursOfDay,
DaysOfWeek: daysOfWeek,
}

parsedParams := d.Get("parameters").(map[string]interface{})
parameters := make(map[string]string)
for k, v := range parsedParams {
parameters[k] = v.(string)
}

schedule, err := c.CreateSchedule(organization, project, name, description, timetable, useSchedulingSystem, parameters)
if err != nil {
return fmt.Errorf("Failed to create schedule: %w", err)
}

d.SetId(schedule.ID)

return resourceCircleCIScheduleRead(d, m)
}

func resourceCircleCIScheduleDelete(d *schema.ResourceData, m interface{}) error {
c := m.(*client.Client)

if err := c.DeleteSchedule(d.Id()); err != nil {
return err
}

d.SetId("")

return nil
}

func resourceCircleCIScheduleRead(d *schema.ResourceData, m interface{}) error {
c := m.(*client.Client)
id := d.Id()

schedule, err := c.GetSchedule(id)
if err != nil {
return fmt.Errorf("Failed to read schedule: %s", id)
}

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

_, organization, project, err := explodeProjectSlug(schedule.ProjectSlug)
if err != nil {
return err
}

d.Set("organization", organization)
d.Set("project", project)
d.Set("name", schedule.Name)
d.Set("description", schedule.Description)
d.Set("per_hour", schedule.Timetable.PerHour)
d.Set("hours_of_day", schedule.Timetable.HoursOfDay)
d.Set("days_of_week", schedule.Timetable.DaysOfWeek)
d.Set("parameters", schedule.Parameters)

if schedule.Actor.ID == scheduledActorID {
d.Set("use_scheduling_system", true)
} else {
d.Set("use_scheduling_system", false)
}

return nil
}

func resourceCircleCIScheduleUpdate(d *schema.ResourceData, m interface{}) error {
c := m.(*client.Client)

id := d.Id()
name := d.Get("name").(string)
description := d.Get("description").(string)
attributionActor := d.Get("use_scheduling_system").(bool)

parsedHours := d.Get("hours_of_day").([]interface{})
var hoursOfDay []uint
for _, hour := range parsedHours {
hoursOfDay = append(hoursOfDay, uint(hour.(int)))
}

var exists = struct{}{}
validDays := make(map[string]interface{})
validDays["MON"] = exists
validDays["TUE"] = exists
validDays["WED"] = exists
validDays["THU"] = exists
validDays["FRI"] = exists
validDays["SAT"] = exists
validDays["SUN"] = exists

parsedDays := d.Get("days_of_week").([]interface{})
var daysOfWeek []string
for _, day := range parsedDays {
if validDays[day.(string)] == nil {
return fmt.Errorf("Invalid day specified: %s", day)
}
daysOfWeek = append(daysOfWeek, day.(string))
}

timetable := api.Timetable{
PerHour: uint(d.Get("per_hour").(int)),
HoursOfDay: hoursOfDay,
DaysOfWeek: daysOfWeek,
}

parsedParams := d.Get("parameters").(map[string]interface{})
parameters := make(map[string]string)
for k, v := range parsedParams {
parameters[k] = v.(string)
}

_, err := c.UpdateSchedule(id, name, description, timetable, attributionActor, parameters)
if err != nil {
return fmt.Errorf("Failed to update schedule: %w", err)
}

return nil
}

func resourceCircleCIScheduleImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
c := m.(*client.Client)

schedule, err := c.GetSchedule(d.Id())
if err != nil {
return nil, err
}

d.SetId(schedule.ID)

return []*schema.ResourceData{d}, nil
}

func explodeProjectSlug(slug string) (string, string, string, error) {
matches := strings.Split(slug, "/")

if len(matches) != 3 {
return "", "", "", fmt.Errorf("Extracting vcs, org, project from project-slug '%s' failed", slug)
}
return matches[0], matches[1], matches[2], nil
}
Loading