From 8da2f163d0302d6f6e508a364b747feb219f1b8d Mon Sep 17 00:00:00 2001 From: Brett Wright Date: Fri, 9 Oct 2020 08:22:23 +0200 Subject: [PATCH] Added data source --- provider/data_project.go | 74 +++++++++++++++++++++++ provider/data_registry.go | 80 +++++++++++++++++++++++++ provider/resource_robot_account_test.go | 5 +- 3 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 provider/data_project.go create mode 100644 provider/data_registry.go diff --git a/provider/data_project.go b/provider/data_project.go new file mode 100644 index 0000000..e245cb4 --- /dev/null +++ b/provider/data_project.go @@ -0,0 +1,74 @@ +package provider + +import ( + "encoding/json" + "fmt" + "strconv" + + "github.com/BESTSELLER/terraform-provider-harbor/client" + "github.com/BESTSELLER/terraform-provider-harbor/models" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +func dataProject() *schema.Resource { + return &schema.Resource{ + Read: dataProjectRead, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "project_id": { + Type: schema.TypeInt, + Computed: true, + }, + "public": { + Type: schema.TypeBool, + Computed: true, + }, + "vulnerability_scanning": { + Type: schema.TypeBool, + Computed: true, + }, + }, + } +} + +func dataProjectRead(d *schema.ResourceData, m interface{}) error { + apiClient := m.(*client.Client) + name := d.Get("name").(string) + projectPath := models.PathProjects + "?name=" + name + + resp, _, err := apiClient.SendRequest("GET", projectPath, nil, 200) + if err != nil { + return err + } + + var jsonData []models.ProjectsBodyResponses + err = json.Unmarshal([]byte(resp), &jsonData) + if err != nil { + return fmt.Errorf("Unable to find the project named: %s", name) + } + + for _, v := range jsonData { + + if v.Name == name { + id := models.PathProjects + "/" + strconv.Itoa(v.ProjectID) + public, err := strconv.ParseBool(v.Metadata.Public) + if err != nil { + return err + } + autoScan, err := strconv.ParseBool(v.Metadata.AutoScan) + if err != nil { + return err + } + + d.SetId(id) + d.Set("project_id", v.ProjectID) + d.Set("name", v.Name) + d.Set("public", public) + d.Set("vulnerability_scanning", autoScan) + } + } + return nil +} diff --git a/provider/data_registry.go b/provider/data_registry.go new file mode 100644 index 0000000..71f7bfe --- /dev/null +++ b/provider/data_registry.go @@ -0,0 +1,80 @@ +package provider + +import ( + "encoding/json" + "fmt" + "strconv" + + "github.com/BESTSELLER/terraform-provider-harbor/client" + "github.com/BESTSELLER/terraform-provider-harbor/models" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +func dataRegistry() *schema.Resource { + return &schema.Resource{ + Read: dataRegistryRead, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "registry_id": { + Type: schema.TypeInt, + Computed: true, + }, + "description": { + Type: schema.TypeString, + Computed: true, + }, + "type": { + Type: schema.TypeString, + Computed: true, + }, + "url": { + Type: schema.TypeString, + Computed: true, + }, + "insecure": { + Type: schema.TypeString, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataRegistryRead(d *schema.ResourceData, m interface{}) error { + apiClient := m.(*client.Client) + name := d.Get("name").(string) + registryPath := models.PathRegistries + "?name=" + name + resp, _, err := apiClient.SendRequest("GET", registryPath, nil, 200) + if err != nil { + return err + } + + var jsonData []models.RegistryBody + err = json.Unmarshal([]byte(resp), &jsonData) + if err != nil { + return fmt.Errorf("Unable to find the registry named: %s", name) + } + + for _, v := range jsonData { + if v.Name == name { + id := models.PathProjects + "/" + strconv.Itoa(v.ID) + + d.SetId(id) + d.Set("registry_id", v.ID) + d.Set("name", v.Name) + d.Set("type", v.Type) + d.Set("description", v.Description) + d.Set("url", v.URL) + d.Set("insecure", v.Insecure) + d.Set("status", v.Status) + } + } + + return nil +} diff --git a/provider/resource_robot_account_test.go b/provider/resource_robot_account_test.go index ca3ef1f..9a3310a 100644 --- a/provider/resource_robot_account_test.go +++ b/provider/resource_robot_account_test.go @@ -25,8 +25,6 @@ func TestAccRobotBasic(t *testing.T) { testAccCheckResourceExists(harborRobotAccount), resource.TestCheckResourceAttr( harborRobotAccount, "name", "test_robot_account"), - // resource.TestCheckResourceAttr( - // harborRobotAccount, "action", "push"), ), }, }, @@ -60,6 +58,9 @@ func testAccCheckRobotDestroy(s *terraform.State) error { if rs.Type != "harbor_robot_account" { continue } + if rs.Type != "harbor_project" { + continue + } resp, _, err := apiClient.SendRequest("GET", rs.Primary.ID, nil, 404) if err != nil {