jdamata / terraform-provider-sonarqube

Terraform provider for managing Sonarqube configuration
GNU General Public License v3.0
65 stars 60 forks source link

Setting block in resource sonarqube_project #254

Closed Malgosiatobiasz1 closed 8 months ago

Malgosiatobiasz1 commented 8 months ago

Hello, are the values ​​we create in the settings block visible somewhere in the project/portfolio from the browser level? What is the use case for the settings block in this resource?

https://registry.terraform.io/providers/jdamata/sonarqube/latest/docs/resources/sonarqube_project#:~:text=setting%20%2D%20(Optional)%20The,field%20setting%20values

freeranger commented 8 months ago

Hello When you look at Project Settings via the UI, they all say something like Key: sonar.inclusions - so sonar.inclusions is the key you would provide in the settings block and then whatever value you want.

The use case for this block is to expose the features provided by the Sonar API to manage project-level settings.

Malgosiatobiasz1 commented 8 months ago

Hi @freeranger where exacly in the project settings I can find Key: sonar.inclusions ?

freeranger commented 8 months ago

Project Settings, General Settings, Analysis Scope

Malgosiatobiasz1 commented 8 months ago

Hi @freeranger I created a test project 7 with a setting block:project7.hcl image projects.tf image

terragrunt apply was successful, but values key = "sonar.inclusions" value = "*/.css"

are not shown in Project Settings, General Settings, Analysis Scope for this project

image

Is this expected behaviour ?

freeranger commented 8 months ago

Why don't you start with something simple and see how it behaves - what if you just modify the example from the provider documentation and do this:

resource "sonarqube_project" "main" {
    name       = "SonarQube"
    project    = "my_project_7"
    visibility = "public" 

    setting {
        key   = "sonar.inclusions"
        value = "**/*.css"
    }
}

Does that work? If not then why not? Maybe this setting needs values rather than value? Unfortunately Sonar don't give you any explicit way to know what type of setting it is - though as a general rule if the UI allows multiple rows for a setting then it is probably values You could use a tool like Postman to explore the API and see what data you get back. Once you have a simple example working, try it in your more complex case

Malgosiatobiasz1 commented 8 months ago

Hi @freeranger , tested with simple config from docs: resource "sonarqube_project" "main" { name = "SonarQube" project = "my_project" visibility = "public" setting { key = "sonar.demo" value = "sonarqube@example.org" } }

this is not working as expected image

freeranger commented 8 months ago

This block (and the Sonar API itself) won’t create made up settings - you can only set values for settings that exist. The docs are meant to show a non-specific, general example rather than a concrete one. If you want to test a single-valued setting then find one and try it with value If you want to test a multi-valued setting then find one and try it with values

Also check that what you are trying to achieve will work with the Sonar API directly - this provider does not have any more capability than the API, so if the API can’t do it then the provider can’t either.

Malgosiatobiasz1 commented 7 months ago

Hi @freeranger from api I was able to set Source File Inclusions with command curl -u token: -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "component=my_project_7&key=sonar.inclusions&values=.xml&values=.py" https://instance_url/api/settings/set image

but I can't do the same with terraform :(

freeranger commented 7 months ago

Hello Whenever you have a problem, you should always post the simplest possible example script that demonstrates your problem. I have no idea if you used value or values for example, and if anything else may be incorrect with your attempt. And some logs showing output would be helpful too.

This is not an official product and relies on the repo owner and people like myself having some spare time to look at issues - so the more info you can provide, the less time people have to spend trying to figure out your problem and the more likely it is that someone will be able to help.

I have explained how the resource should work, and the difference between value and values` and you have basically come back with "it doesn't work" - which doesn't help anyone understand your problem.

Malgosiatobiasz1 commented 6 months ago

Hi @freeranger , @pnag90 I tried to use the settings block for the project, below is the code:

terraform {
  required_providers {
    sonarqube = {
      source = "jdamata/sonarqube"
      version = "0.16.9"
    }
  }
}

provider "sonarqube" {
  token             = "token"
  host              = "https://my-sonarqube-instance.com/"
  installed_version = "10.2.1"
  installed_edition = "Enterprise"

}

resource "sonarqube_project" "main" {
    name       = "SonarQube_setting_block_test"
    project    = "my_project"
    visibility = "public" 

    setting {
        key   = "sonar.inclusions"
        value = ".xml"
    }
    setting {
        key   = "sonar.test.exclusions"
        values = [".java",".js"]
    }
    setting {
        key           = "sonar.test.inclusions"
        field_values  = [
            {
                "0" = ".py"
            },
            {
                "1" = ".tf"
            }
        ]
    }
}

terraform apply did not show any errors, logs:

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # sonarqube_project.main will be created
  + resource "sonarqube_project" "main" {
      + id         = (known after apply)
      + name       = "SonarQube_setting_block_test"
      + project    = "my_project"
      + visibility = "public"

      + setting {
          + key   = "sonar.inclusions"
          + value = ".xml"
        }
      + setting {
          + key    = "sonar.test.exclusions"
          + values = [
              + ".java",
              + ".js",
            ]
        }
      + setting {
          + field_values = [
              + {
                  + "0" = ".py"
                },
              + {
                  + "1" = ".tf"
                },
            ]
          + key          = "sonar.test.inclusions"
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

sonarqube_project.main: Creating...
sonarqube_project.main: Creation complete after 1s [id=my_project]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Unfortunately, the settings are not visible in the project:

image

freeranger commented 6 months ago

Hello When I tried your example I did get errors from plan because you are trying to use incorrect setting. You don;t get to choose if it is value or values or field_values that you fill in - you have to use the correct format for the setting you are trying to update. This is outside the control of the provider and something you would have to take up with Sonar themselves. Defining the resource correctly:

resource "sonarqube_project" "main" {
    name       = "SonarQube_setting_block_test"
    project    = "my_project"
    visibility = "public"

    setting {
        key   = "sonar.inclusions"
        values = [".xml"]
    }
    setting {
        key   = "sonar.test.exclusions"
        values = [".java",".js"]
    }
    setting {
        key           = "sonar.test.inclusions"
        values  = ["foo","bar"]
    }
}

I see the project settings are set correctly in my test instance: image

Malgosiatobiasz1 commented 6 months ago

@freeranger Hi, I tried running terraform with your settings blocks:

resource "sonarqube_project" "main" {
    name       = "SonarQube_setting_block_test"
    project    = "my_project"
    visibility = "public"

    setting {
        key   = "sonar.inclusions"
        values = [".xml"]
    }
    setting {
        key   = "sonar.test.exclusions"
        values = [".java",".js"]
    }
    setting {
        key           = "sonar.test.inclusions"
        values  = ["foo","bar"]
    }
}

Unfortunately, on my sonarqube instance I can't see it from the UI level.

I ran terraform in debug mode, terraform apply was successful, but the following warning appeared:

2024-06-06T11:35:38.748-0400 [WARN]  Provider "registry.terraform.io/jdamata/sonarqube" produced an unexpected new value for sonarqube_project.main during refresh.
      - .setting[1].field_values: was null, but now cty.ListValEmpty(cty.Map(cty.String))
      - .setting[2].field_values: was null, but now cty.ListValEmpty(cty.Map(cty.String))
2024-06-06T11:35:38.752-0400 [WARN]  Provider "registry.terraform.io/jdamata/sonarqube" produced an invalid plan for sonarqube_project.main, but we are tolerating it because it is using the legacy plugin SDK.
    The following problems may be the cause of any confusing errors from downstream operations:
      - .setting[0].field_values: planned value cty.ListValEmpty(cty.Map(cty.String)) for a non-computed attribute
      - .setting[0].value: planned value cty.StringVal("") for a non-computed attribute
      - .setting[1].value: planned value cty.StringVal("") for a non-computed attribute
      - .setting[1].field_values: planned value cty.ListValEmpty(cty.Map(cty.String)) for a non-computed attribute
      - .setting[2].field_values: planned value cty.ListValEmpty(cty.Map(cty.String)) for a non-computed attribute
      - .setting[2].value: planned value cty.StringVal("") for a non-computed attribute
2024-06-06T11:35:38.753-0400 [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = error reading from server: EOF"
2024-06-06T11:35:38.755-0400 [DEBUG] provider: plugin process exited: path=.terraform/providers/registry.terraform.io/jdamata/sonarqube/0.16.9/linux_amd64/terraform-provider-sonarqube_v0.16.9 pid=10724
2024-06-06T11:35:38.755-0400 [DEBUG] provider: plugin exited
2024-06-06T11:35:38.755-0400 [DEBUG] no planned changes, skipping apply graph check
2024-06-06T11:35:38.755-0400 [INFO]  backend/local: plan operation completed

Sonarqube edition : DCE Sonarqube version : 10.2.1

freeranger commented 6 months ago

I was using the latest version of the provider and a fresh Sonar 10.5.1 Enterprise Edition running in a container. You could try the newer version of the plugin to start with and if still no luck, try a fresh Sonar install somewhere (running in a container is easiest IMO)to test it out. ISTR something to do with settings was addressed in 0.16.10 so that’s where I would start….

Malgosiatobiasz1 commented 6 months ago

@freeranger Working as expected with : Sonarqube edition : DCE Sonarqube version : 10.2.1 provider version 0.16.10