Skip to content

Commit

Permalink
harbor_config_system: add scanner_skip_update_pulltime + detect drift (
Browse files Browse the repository at this point in the history
…#332)

#326

---------

Signed-off-by: flbla <flbla@users.noreply.github.com>
  • Loading branch information
flbla authored May 22, 2023
1 parent 002b8c3 commit 57b70f4
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 1 deletion.
1 change: 1 addition & 0 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func GetConfigSystem(d *schema.ResourceData) models.ConfigBodySystemPost {
body = models.ConfigBodySystemPost{
ProjectCreationRestriction: d.Get("project_creation_restriction").(string),
ReadOnly: d.Get("read_only").(bool),
ScannerSkipUpdatePulltime: d.Get("scanner_skip_update_pulltime").(bool),
RobotTokenDuration: d.Get("robot_token_expiration").(int),
QuotaPerProjectEnable: true,
RobotNamePrefix: d.Get("robot_name_prefix").(string),
Expand Down
4 changes: 3 additions & 1 deletion docs/resources/config_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ The following arguments are supported:
* **robot_token_expiration** - (Optional) The amount of time in days a robot account will expiry.

* **robot_name_prefix** - (Optional) Robot account prefix.
`NOTE: If the time is set to high you will get a 500 internal server error message when creating robot accounts`

`NOTE: If the time is set to high you will get a 500 internal server error message when creating robot accounts`
* **scanner_skip_update_pulltime** - (Optional) Whether or not to skip update pull time for scanner.
`NOTE: "scanner_skip_update_pulltime" can only be used with harbor version v2.8.0 and above`
9 changes: 9 additions & 0 deletions models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type ConfigBodySystemPost struct {
QuotaPerProjectEnable bool `json:"quota_per_project_enable"`
RobotNamePrefix string `json:"robot_name_prefix,omitempty"`
StoragePerProject string `json:"storage_per_project,omitempty"`
ScannerSkipUpdatePulltime bool `json:"scanner_skip_update_pulltime"`
}

type ConfigBodyEmailPost struct {
Expand Down Expand Up @@ -228,8 +229,16 @@ type ConfigBodyResponse struct {
Editable bool `json:"editable,omitempty"`
Value string `json:"value,omitempty"`
} `json:"robot_name_prefix,omitempty"`
RobotTokenDuration struct {
Editable bool `json:"editable,omitempty"`
Value int `json:"value,omitempty"`
} `json:"robot_token_duration,omitempty"`
LdapVerifyCert struct {
Editable bool `json:"editable,omitempty"`
Value bool `json:"value,omitempty"`
} `json:"ldap_verify_cert,omitempty"`
ScannerSkipUpdatePulltime struct {
Editable bool `json:"editable,omitempty"`
Value bool `json:"value,omitempty"`
} `json:"scanner_skip_update_pulltime,omitempty"`
}
5 changes: 5 additions & 0 deletions provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"fmt"
"os"
"strings"
"testing"

"github.com/goharbor/terraform-provider-harbor/client"
Expand Down Expand Up @@ -53,6 +54,10 @@ func testAccCheckResourceExists(resource string) resource.TestCheckFunc {
}
name := rs.Primary.ID

if strings.HasPrefix(name, "configuration/") {
name = "/configurations"
}

apiClient := testAccProvider.Meta().(*client.Client)
_, _, _, err := apiClient.SendRequest("GET", name, nil, 200)
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions provider/resource_config_system.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package provider

import (
"encoding/json"
"fmt"

"github.com/goharbor/terraform-provider-harbor/client"
"github.com/goharbor/terraform-provider-harbor/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -27,6 +30,12 @@ func resourceConfigSystem() *schema.Resource {
"robot_name_prefix": {
Type: schema.TypeString,
Optional: true,
Default: "robot$",
},
"scanner_skip_update_pulltime": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
Create: resourceConfigSystemCreate,
Expand All @@ -50,6 +59,26 @@ func resourceConfigSystemCreate(d *schema.ResourceData, m interface{}) error {
}

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

resp, _, respCode, err := apiClient.SendRequest("GET", models.PathConfig, nil, 200)
if respCode == 404 && err != nil {
d.SetId("")
return fmt.Errorf("Error getting system configuration %s", err)
}

var jsonData models.ConfigBodyResponse
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
return fmt.Errorf("Error getting system configuration %s", err)
}

d.Set("project_creation_restriction", jsonData.ProjectCreationRestriction.Value)
d.Set("read_only", jsonData.ReadOnly.Value)
d.Set("robot_token_expiration", jsonData.RobotTokenDuration.Value)
d.Set("robot_name_prefix", jsonData.RobotNamePrefix.Value)
d.Set("scanner_skip_update_pulltime", jsonData.ScannerSkipUpdatePulltime.Value)

d.SetId("configuration/system")
return nil
}
Expand Down
68 changes: 68 additions & 0 deletions provider/resource_config_system_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package provider

import (
"fmt"
"testing"

"github.com/goharbor/terraform-provider-harbor/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

const resourceConfigSystemMain = "harbor_config_system.main"

func testAccCheckConfigSystemDestroy(s *terraform.State) error {
apiClient := testAccProvider.Meta().(*client.Client)
r := "harbor_config_system"

for _, rs := range s.RootModule().Resources {
if rs.Type != r {
continue
}

resp, _, _, err := apiClient.SendRequest("GET", "/configurations", nil, 200)
if err != nil {
return fmt.Errorf("Resource was not deleted\n%s", resp)
}
}

return nil
}

func TestAccConfigSystem(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckConfigSystemDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckConfigSystem(),
Check: resource.ComposeTestCheckFunc(
testAccCheckResourceExists(resourceConfigSystemMain),
resource.TestCheckResourceAttr(
resourceConfigSystemMain, "project_creation_restriction", "adminonly"),
resource.TestCheckResourceAttr(
resourceConfigSystemMain, "read_only", "false"),
resource.TestCheckResourceAttr(
resourceConfigSystemMain, "robot_token_expiration", "30"),
resource.TestCheckResourceAttr(
resourceConfigSystemMain, "robot_name_prefix", "robot$"),
resource.TestCheckResourceAttr(
resourceConfigSystemMain, "scanner_skip_update_pulltime", "false"),
),
},
},
})
}

func testAccCheckConfigSystem() string {
return fmt.Sprintf(`
resource "harbor_config_system" "main" {
project_creation_restriction = "adminonly"
read_only = false
robot_token_expiration = 30
robot_name_prefix = "robot$"
scanner_skip_update_pulltime = false
}
`)
}

0 comments on commit 57b70f4

Please sign in to comment.