Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/datadrivers/terraform-provider-nexus

go 1.22.1
go 1.23.0

toolchain go1.23.6

require (
Expand Down Expand Up @@ -251,3 +252,6 @@ require (
mvdan.cc/gofumpt v0.7.0 // indirect
mvdan.cc/unparam v0.0.0-20240528143540-8a5130ca722f // indirect
)

replace github.com/datadrivers/go-nexus-client v1.14.0 => ../go-nexus-client
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for local dev

replace github.com/datadrivers/go-nexus-client/nexus3/schema v1.14.0 => ../go-nexus-client/nexus3/schema
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ github.com/cyphar/filepath-securejoin v0.2.5 h1:6iR5tXJ/e6tJZzzdMc1km3Sa7RRIVBKA
github.com/cyphar/filepath-securejoin v0.2.5/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
github.com/daixiang0/gci v0.13.5 h1:kThgmH1yBmZSBCh1EJVxQ7JsHpm5Oms0AMed/0LaH4c=
github.com/daixiang0/gci v0.13.5/go.mod h1:12etP2OniiIdP4q+kjUGrC/rUagga7ODbqsom5Eo5Yk=
github.com/datadrivers/go-nexus-client v1.14.0 h1:95G0EYQ622BemmFHkXgczQh3Vk0wJqIHLxY4HQjcNHQ=
github.com/datadrivers/go-nexus-client v1.14.0/go.mod h1:nxzeU46NnivQUXBwc/Id5dyCsGNv3355CTm7TVzKhXs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
3 changes: 3 additions & 0 deletions internal/provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/datadrivers/terraform-provider-nexus/internal/services/other"
"github.com/datadrivers/terraform-provider-nexus/internal/services/repository"
"github.com/datadrivers/terraform-provider-nexus/internal/services/security"
"github.com/datadrivers/terraform-provider-nexus/internal/services/task"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -79,6 +80,7 @@ func Provider() *schema.Provider {
"nexus_privilege_repository_view": security.DataSourceSecurityPrivilegeRepositoryView(),
"nexus_privilege_repository_admin": security.DataSourceSecurityPrivilegeRepositoryAdmin(),
"nexus_privilege_repository_content_selector": security.DataSourceSecurityPrivilegeRepositoryContentSelector(),
"nexus_task": task.DataSourceTask(),
},
ResourcesMap: map[string]*schema.Resource{
"nexus_blobstore_azure": blobstore.ResourceBlobstoreAzure(),
Expand Down Expand Up @@ -145,6 +147,7 @@ func Provider() *schema.Provider {
"nexus_privilege_repository_admin": security.ResourceSecurityPrivilegeRepositoryAdmin(),
"nexus_privilege_repository_content_selector": security.ResourceSecurityPrivilegeRepositoryContentSelector(),
"nexus_privilege_wildcard": security.ResourceSecurityPrivilegeWildcard(),
"nexus_task": task.ResourceTask(),
},
Schema: map[string]*schema.Schema{
"insecure": {
Expand Down
34 changes: 34 additions & 0 deletions internal/services/task/data_source_task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package task

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DataSourceTask() *schema.Resource {
return &schema.Resource{
Read: dataSourceTaskRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}

func dataSourceTaskRead(d *schema.ResourceData, m interface{}) error {
d.SetId(d.Get("id").(string))
return resourceTaskRead(d, m)
}
170 changes: 170 additions & 0 deletions internal/services/task/resource_task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package task

import (
nexus "github.com/datadrivers/go-nexus-client/nexus3"
nexusSchema "github.com/datadrivers/go-nexus-client/nexus3/schema/task"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func ResourceTask() *schema.Resource {
return &schema.Resource{
Create: resourceTaskCreate,
Read: resourceTaskRead,
Update: resourceTaskUpdate,
Delete: resourceTaskDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Required: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"alert_email": {
Type: schema.TypeString,
Optional: true,
Default: nil,
},
"notification_condition": {
Type: schema.TypeString,
Required: true,
},
"frequency": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"schedule": {
Type: schema.TypeString,
Required: true,
},
"start_date": {
Type: schema.TypeInt,
Required: true,
},
"time_zone_offset": {
Type: schema.TypeString,
Optional: true,
},
"recurring_days": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
},
"cron_expression": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
"properties": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func resourceTaskCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*nexus.NexusClient)
frequencySet := d.Get("frequency").(*schema.Set)
frequencyMap := frequencySet.List()[0].(map[string]interface{})
task := nexusSchema.TaskCreateStruct{
Name: d.Get("name").(string),
Type: d.Get("type").(string),
Enabled: d.Get("enabled").(bool),
AlertEmail: d.Get("alert_email").(string),
NotificationCondition: d.Get("notification_condition").(string),
Frequency: nexusSchema.FrequencyXO{
Schedule: frequencyMap["schedule"].(string),
StartDate: frequencyMap["start_date"].(int),
TimeZoneOffset: frequencyMap["time_zone_offset"].(string),
RecurringDays: frequencyMap["recurring_days"].([]interface{}),
CronExpression: frequencyMap["cron_expression"].(string),
},
Properties: d.Get("properties").(map[string]interface{}),
}

newTask, err := client.Task.CreateTask(&task)
if err != nil {
return err
}
d.SetId(newTask.ID)
return resourceTaskRead(d, m)
}

func resourceTaskRead(d *schema.ResourceData, m interface{}) error {
client := m.(*nexus.NexusClient)
id := d.Id()

task, err := client.Task.GetTask(id)
if err != nil {
return err
}

d.Set("name", task.Name)
d.Set("type", task.Type)

return nil
}

func resourceTaskUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(*nexus.NexusClient)
if d.HasChange("name") || d.HasChange("enabled") || d.HasChange("Frequency") || d.HasChange("notification_condition") {
task := getTaskFromResourceData(d)
if err := client.Task.UpdateTask(d.Id(), &task); err != nil {
return err
}
}
task := getTaskFromResourceData(d)

err := client.Task.UpdateTask(d.Id(), &task)
if err != nil {
return err
}
return resourceTaskRead(d, m)
}

func getTaskFromResourceData(d *schema.ResourceData) nexusSchema.TaskCreateStruct {
frequencySet := d.Get("frequency").(*schema.Set)
frequencyMap := frequencySet.List()[0].(map[string]interface{})
return nexusSchema.TaskCreateStruct{
Name: d.Get("name").(string),
Enabled: d.Get("enabled").(bool),
AlertEmail: d.Get("alert_email").(string),
NotificationCondition: d.Get("notification_condition").(string),
Frequency: nexusSchema.FrequencyXO{
Schedule: frequencyMap["schedule"].(string),
StartDate: frequencyMap["start_date"].(int),
TimeZoneOffset: frequencyMap["time_zone_offset"].(string),
RecurringDays: frequencyMap["recurring_days"].([]interface{}),
CronExpression: frequencyMap["cron_expression"].(string),
},
}
}

func resourceTaskDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*nexus.NexusClient)

if err := client.Task.DeleteTask(d.Id()); err != nil {
return err
}

d.SetId("")
return nil
}
81 changes: 81 additions & 0 deletions internal/services/task/resource_task_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package task_test

import (
"fmt"
"strconv"
"strings"
"testing"

"github.com/datadrivers/go-nexus-client/nexus3/schema/task"
"github.com/datadrivers/terraform-provider-nexus/internal/acceptance"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccResourceTask_basic(t *testing.T) {
resourceName := "nexus_task.test"

task := task.TaskCreateStruct{
Name: acctest.RandString(20),
Type: "tags.cleanup",
Enabled: true,
AlertEmail: acctest.RandString(20) + "@example.com",
NotificationCondition: "FAILURE",
Frequency: task.FrequencyXO{
StartDate: 1740122615,
Schedule: "daily",
TimeZoneOffset: "+1",
//RecurringDays: []{1, 2, 3},
CronExpression: "0 0 * * *",
},
Properties: map[string]interface{}{},
}

resource.Test(t, resource.TestCase{
PreCheck: func() { acceptance.AccPreCheck(t) },
Providers: acceptance.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccResourceTaskCreateConfig(task),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", task.Name),
resource.TestCheckResourceAttr(resourceName, "type", task.Type),
resource.TestCheckResourceAttr(resourceName, "alert_email", task.AlertEmail),
resource.TestCheckResourceAttr(resourceName, "notification_condition", task.NotificationCondition),
resource.TestCheckResourceAttr(resourceName, "frequency.0.schedule", task.Frequency.Schedule),
resource.TestCheckResourceAttr(resourceName, "frequency.0.start_date", strconv.FormatInt(int64(task.Frequency.StartDate), 10)),
resource.TestCheckResourceAttr(resourceName, "frequency.0.time_zone_offset", task.Frequency.TimeZoneOffset),
resource.TestCheckResourceAttr(resourceName, "frequency.0.cron_expression", task.Frequency.CronExpression),
),
},
},
})
}

func testAccResourceTaskCreateConfig(task task.TaskCreateStruct) string {
return fmt.Sprintf(`
resource "nexus_task" "test" {
name = "%s"
type = "%s"
enabled = %t
alert_email = "%s"
notification_condition = "%s"
frequency {
schedule = "%s"
start_date = %d
time_zone_offset = "%s"
recurring_days = [%s]
cron_expression = "%s"
}
properties = {}
}
`, task.Name, task.Type, task.Enabled, task.AlertEmail, task.NotificationCondition, task.Frequency.Schedule, task.Frequency.StartDate, task.Frequency.TimeZoneOffset, formatRecurringDays(task.Frequency.RecurringDays), task.Frequency.CronExpression)
}

func formatRecurringDays(days []interface{}) string {
var formattedDays []string
for _, day := range days {
formattedDays = append(formattedDays, strconv.Itoa(day.(int)))
}
return strings.Join(formattedDays, ", ")
}