diff --git a/docs/data-sources/project.md b/docs/data-sources/project.md new file mode 100644 index 0000000..a79befe --- /dev/null +++ b/docs/data-sources/project.md @@ -0,0 +1,26 @@ +# Data Source: harbor_project + +## Example Usage +```hcl +data "haror_project" "main" { + name = "library" +} + +output "project_id" { + value = data.harbor_project.main.id +} +``` + +## Argument Reference +The following arguments are supported: + +* **name** - (Required) The of the project that will be created in harbor. + +## Attributes Reference +In addition to all argument, the folloing attributes are exported: + +* **project_id** - The id of the project within harbor. + +* **public** - If the project will be public accessibility. + +* **vulnerability_scanning** - If the images will be scanned for vulnerabilities when push to harbor. \ No newline at end of file diff --git a/docs/data-sources/registry.md b/docs/data-sources/registry.md new file mode 100644 index 0000000..e3053b7 --- /dev/null +++ b/docs/data-sources/registry.md @@ -0,0 +1,33 @@ +# Data Source: harbor_registry + +## Example Usage + +```hcl +data "harbor_registry" "main" { + name = "test_docker_harbor" +} + +output "harbor_registry_id" { + value = data.harbor_registry.main.id +} +``` + +## Argument Reference +The following arguments are supported: + +* **name** - (Required) The name of the register. + +## Attributes Reference +In addition to all argument, the folloing attributes are exported: + +* **registry_id** - The id of the register within harbor. + +* **status** - The health status of the external container register + +* **endpoint_url** - The url endpoint for the external container register + +* **description** - The description of the external container register. + +* **insecure** - If the certificate of the external container register can be verified. + +* **type** - The type of the provider type. \ No newline at end of file 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/provider.go b/provider/provider.go index 6a5e46a..1b2202f 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -55,6 +55,10 @@ func Provider() terraform.ResourceProvider { "harbor_registry": resourceRegistry(), "harbor_replication": resourceReplication(), }, + DataSourcesMap: map[string]*schema.Resource{ + "harbor_project": dataProject(), + "harbor_registry": dataRegistry(), + }, ConfigureFunc: providerConfigure, } 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 {