From bddd089ad0d20c8c28b088977e4c6e052de9190b Mon Sep 17 00:00:00 2001 From: Florian Blampey Date: Fri, 29 Sep 2023 11:39:34 +0200 Subject: [PATCH 1/2] update docker version (24.0) for tests (#375) Signed-off-by: flbla --- .github/workflows/test.yml | 16 ++++++++++------ provider/resource_replication_test.go | 4 ++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2880dec0..cc552cf6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -78,6 +78,7 @@ jobs: - '1.2.*' - '1.3.*' - '1.4.*' + - '1.5.*' steps: - name: Check out code into the Go module directory @@ -86,16 +87,16 @@ jobs: - name: Setup Docker uses: docker-practice/actions-setup-docker@v1 with: - docker_version: "20.10" + docker_version: "24.0" docker_channel: stable docker_daemon_json: '{"insecure-registries":["0.0.0.0/0"]}' - name: Create kind cluster uses: helm/kind-action@v1.8.0 with: - version: v0.11.1 - node_image: kindest/node:v1.22.0 - cluster_name: kind-cluster-v1.22.0 + version: v0.20.0 + node_image: kindest/node:v1.28.0 + cluster_name: kind-cluster-v1.28.0 config: kind-cluster.yaml - name: Install Nginx ingress controller @@ -126,10 +127,12 @@ jobs: - name: Wait for harbor to be ready run: | attempt_counter=0 - max_attempts=120 # Max wait will be 10m - while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' $HARBOR_URL)" != "401" ]]; do + max_attempts=25 # Max wait will be 2m + while [[ "$(curl -k -s -o /dev/null -w ''%{http_code}'' $HARBOR_URL/api/v2.0/configurations -u $HARBOR_USERNAME:$HARBOR_PASSWORD)" != "200" ]]; do if [ ${attempt_counter} -eq ${max_attempts} ];then echo "Max attempts reached" + kubectl get pods -A + curl -v $HARBOR_URL exit 1 fi @@ -137,6 +140,7 @@ jobs: attempt_counter=$(($attempt_counter+1)) sleep 5 done + sleep 5 - name: Set up Go uses: actions/setup-go@v4 diff --git a/provider/resource_replication_test.go b/provider/resource_replication_test.go index ad68bd4f..4793b6ac 100644 --- a/provider/resource_replication_test.go +++ b/provider/resource_replication_test.go @@ -103,7 +103,7 @@ func testAccCheckReplicationUpdate() string { } func TestDestinationNamespace(t *testing.T) { - var scheduleType = "* 0/15 * * * *" + var scheduleType = "0 0/15 * * * *" var destNamepace = "gcp-project" resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -125,7 +125,7 @@ func TestDestinationNamespace(t *testing.T) { } func TestDestinationNamespaceReplaceCount(t *testing.T) { - var scheduleType = "* 0/15 * * * *" + var scheduleType = "0 0/15 * * * *" var destNamespaceReplaceCount = 0 resource.Test(t, resource.TestCase{ From 773a1fdce8664dfec13189a043f08f05bd9124a7 Mon Sep 17 00:00:00 2001 From: David Berndtsson Date: Fri, 29 Sep 2023 11:53:03 +0200 Subject: [PATCH 2/2] Error handling project and members (#356) This PR introduces error-handling to gracefully update and recreate resources that have been altered outside of Terraform. - project - project_member_group - project_member_user This fixes #235 --------- Signed-off-by: David Berndtsson Signed-off-by: flbla Co-authored-by: flbla --- provider/resource_project.go | 15 ++++++++------- provider/resource_project_member_group.go | 13 +++++++------ provider/resource_project_member_user.go | 15 +++++++++------ 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/provider/resource_project.go b/provider/resource_project.go index 85f25dcf..a598e016 100644 --- a/provider/resource_project.go +++ b/provider/resource_project.go @@ -89,7 +89,7 @@ func resourceProjectCreate(d *schema.ResourceData, m interface{}) error { return err } - id, err := client.GetID(headers) + id, _ := client.GetID(headers) d.SetId(id) return resourceProjectRead(d, m) } @@ -100,15 +100,16 @@ func resourceProjectRead(d *schema.ResourceData, m interface{}) error { resp, _, respCode, err := apiClient.SendRequest("GET", d.Id(), nil, 200) if respCode == 404 && err != nil { d.SetId("") - return fmt.Errorf("Resource not found %s", d.Id()) + return nil + } else if err != nil { + return fmt.Errorf("resource not found %s", d.Id()) } var jsonData models.ProjectsBodyResponses err = json.Unmarshal([]byte(resp), &jsonData) if err != nil { - return fmt.Errorf("Resource not found %s", d.Id()) + return fmt.Errorf("resource not found %s", d.Id()) } - autoScan := jsonData.Metadata.AutoScan var vuln bool if autoScan == "" { @@ -186,12 +187,12 @@ func resourceProjectDelete(d *schema.ResourceData, m interface{}) error { projectName := d.Get("name").(string) repos, _ := apiClient.GetProjectRepositories(projectName) if len(repos) != 0 { - return fmt.Errorf("Project %s is not empty, please set force_delete to TRUE to clean all repositories", projectName) + return fmt.Errorf("project %s is not empty, please set force_delete to TRUE to clean all repositories", projectName) } } - _, _, _, err := apiClient.SendRequest("DELETE", d.Id(), nil, 200) - if err != nil { + _, _, respCode, err := apiClient.SendRequest("DELETE", d.Id(), nil, 200) + if respCode != 404 && err != nil { // We can't delete something that doesn't exist. Hence the 404-check return err } return nil diff --git a/provider/resource_project_member_group.go b/provider/resource_project_member_group.go index 0bb4a91b..d7c0f441 100644 --- a/provider/resource_project_member_group.go +++ b/provider/resource_project_member_group.go @@ -101,15 +101,16 @@ func resourceMembersGroupRead(d *schema.ResourceData, m interface{}) error { resp, _, respCode, err := apiClient.SendRequest("GET", d.Id(), nil, 200) if respCode == 404 && err != nil { d.SetId("") - return fmt.Errorf("Resource not found %s", d.Id()) + return nil + } else if err != nil { + return fmt.Errorf("resource not found %s", d.Id()) } var jsonData models.ProjectMembersBodyResponses err = json.Unmarshal([]byte(resp), &jsonData) if err != nil { - return fmt.Errorf("Resource not found %s", d.Id()) + return fmt.Errorf("resource not found %s", d.Id()) } - d.Set("role", client.RoleTypeNumber(jsonData.RoleID)) d.Set("project_id", checkProjectid(strconv.Itoa(jsonData.ProjectID))) d.Set("group_name", jsonData.EntityName) @@ -131,9 +132,9 @@ func resourceMembersGroupUpdate(d *schema.ResourceData, m interface{}) error { func resourceMembersGroupDelete(d *schema.ResourceData, m interface{}) error { apiClient := m.(*client.Client) - _, _, _, err := apiClient.SendRequest("DELETE", d.Id(), nil, 200) - if err != nil { - fmt.Println(err) + _, _, respCode, err := apiClient.SendRequest("DELETE", d.Id(), nil, 200) + if respCode != 404 && err != nil { // We can't delete something that doesn't exist. Hence the 404-check + return err } return nil } diff --git a/provider/resource_project_member_user.go b/provider/resource_project_member_user.go index dbbf984a..cc9bb186 100644 --- a/provider/resource_project_member_user.go +++ b/provider/resource_project_member_user.go @@ -80,17 +80,20 @@ func resourceMembersUserRead(d *schema.ResourceData, m interface{}) error { resp, _, respCode, err := apiClient.SendRequest("GET", d.Id(), nil, 200) if respCode == 404 && err != nil { d.SetId("") - return fmt.Errorf("Resource not found %s", d.Id()) + return nil + } else if err != nil { + return fmt.Errorf("resource not found %s", d.Id()) } + var jsonData models.ProjectMembersBodyResponses err = json.Unmarshal([]byte(resp), &jsonData) if err != nil { - return fmt.Errorf("Resource not found %s", d.Id()) + return fmt.Errorf("resource not found %s", d.Id()) } - d.Set("role", client.RoleTypeNumber(jsonData.RoleID)) d.Set("project_id", checkProjectid(strconv.Itoa(jsonData.ProjectID))) d.Set("user_name", jsonData.EntityName) + return nil } @@ -109,9 +112,9 @@ func resourceMembersUserUpdate(d *schema.ResourceData, m interface{}) error { func resourceMembersUserDelete(d *schema.ResourceData, m interface{}) error { apiClient := m.(*client.Client) - _, _, _, err := apiClient.SendRequest("DELETE", d.Id(), nil, 200) - if err != nil { - fmt.Println(err) + _, _, respCode, err := apiClient.SendRequest("DELETE", d.Id(), nil, 200) + if respCode != 404 && err != nil { // We can't delete something that doesn't exist. Hence the 404-check + return err } return nil }