Skip to content

Commit

Permalink
feat: [CDS-84451]: add proxy config (#760)
Browse files Browse the repository at this point in the history
* feat: [CDS-84451]: add proxy config

Signed-off-by: Mirko Teodorovic <mirko.teodorovic@harness.io>

* feat: [CDS-84451]: fix tests

Signed-off-by: Mirko Teodorovic <mirko.teodorovic@harness.io>

* feat: [CDS-84451]: add proxy config, update mod

Signed-off-by: Mirko Teodorovic <mirko.teodorovic@harness.io>

* feat: [CDS-84451]: update go mod

Signed-off-by: Mirko Teodorovic <mirko.teodorovic@harness.io>

---------

Signed-off-by: Mirko Teodorovic <mirko.teodorovic@harness.io>
  • Loading branch information
mteodor committed Nov 28, 2023
1 parent 7730612 commit c8704e3
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package agent_yaml

import (
"context"
"github.com/antihax/optional"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
Expand Down Expand Up @@ -47,6 +46,40 @@ func DataSourceGitopsAgentDeployYaml() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"ca_data": {
Description: "CA data of the GitOps agent, base64 encoded content of ca chain.",
Type: schema.TypeString,
Optional: true,
},
"proxy": {
Description: "Proxy settings for the GitOps agent.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"http": {
Description: "HTTP proxy settings for the GitOps agent.",
Type: schema.TypeString,
Optional: true,
},
"https": {
Description: "HTTPS proxy settings for the GitOps agent.",
Type: schema.TypeString,
Optional: true,
},
"username": {
Description: "Username for the proxy.",
Type: schema.TypeString,
Optional: true,
},
"password": {
Description: "Password for the proxy.",
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
}
return resource
Expand All @@ -56,11 +89,46 @@ func dataSourceGitopsAgentDeployYamlRead(ctx context.Context, d *schema.Resource
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
agentIdentifier := d.Get("identifier").(string)

resp, httpResp, err := c.AgentApi.AgentServiceForServerGetDeployYaml(ctx, agentIdentifier, c.AccountId, &nextgen.AgentsApiAgentServiceForServerGetDeployYamlOpts{
OrgIdentifier: optional.NewString(d.Get("org_id").(string)),
ProjectIdentifier: optional.NewString(d.Get("project_id").(string)),
Namespace: optional.NewString(d.Get("namespace").(string)),
})
resp, httpResp, err := c.AgentApi.AgentServiceForServerPostDeployYaml(ctx, func() nextgen.V1AgentYamlQuery {
var yamlQuery nextgen.V1AgentYamlQuery
if attr, ok := d.GetOk("account_id"); ok {
yamlQuery.AccountIdentifier = attr.(string)
}
if attr, ok := d.GetOk("project_id"); ok {
yamlQuery.ProjectIdentifier = attr.(string)
}
if attr, ok := d.GetOk("org_id"); ok {
yamlQuery.OrgIdentifier = attr.(string)
}
if attr, ok := d.GetOk("namespace"); ok {
yamlQuery.Namespace = attr.(string)
}
if attr, ok := d.GetOk("ca_data"); ok {
yamlQuery.CaData = attr.(string)
}

if attr, ok := d.GetOk("proxy"); ok {
proxy := attr.([]interface{})
if attr != nil && len(proxy) > 0 {
p := proxy[0].(map[string]interface{})
var v1Proxy nextgen.V1Proxy
if p["http"] != nil {
v1Proxy.Http = p["http"].(string)
}
if p["https"] != nil {
v1Proxy.Https = p["https"].(string)
}
if p["username"] != nil {
v1Proxy.Username = p["username"].(string)
}
if p["password"] != nil {
v1Proxy.Password = p["password"].(string)
}
yamlQuery.Proxy = &v1Proxy
}
}
return yamlQuery
}(), agentIdentifier)

if err != nil {
return helpers.HandleApiError(err, d, httpResp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ func testAccDataSourceGitopsAgentDeployYaml(agentId string, accountId string, ag
project_id = harness_platform_project.test.id
org_id = harness_platform_organization.test.id
namespace = "%[4]s"
ca_data = "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURuekNDQW9lZ0F3SUJBZ0lVYm8wQmJSU2IrYWE3O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Qzb3dId1lEVlIwakJCZ3dGb0FVdjZlTHdDK1QKRjBHSzlyamx0TVEwMzh3NFQzb3dEd1lEVlIwVEFRSC9CQVV3QXdFQi96QU5CZ2txaGtpRzl3MEJBUXNGQUFPQwpBUUVBSGVyWGc1a2hEVkxpWG9ZSmpRMnhTQ2xoQlVIdGdSTGJ6R25ZekJ1R3VseTh1UW9BZ1dLZU1kM0pjSk93CmJ4K3c3NzFsUzFNbmdENEhiK0ZXWWxkdE5xUHZQa2c3RXZKb2lFMHQzSElzck02WXdyNDUvNHBSZVBMWU1paSsKV0FqTFhOZGVuUUUwVlFvY2pzKzN4M0QyK0FOYitRUTFxVTAzYVhiSEVRQzdmU2k1Y2pPMjd1aWRocVoyNEtHbQpJOE0vN0VmRWhWR01LeStKYnd3WGdYaVZvQ2FHK3QyUTRHS3NETlNJRlNWVEpsT1JPbXBYUUVZdmxIMmRndzdkClVieWRZR1l0TXlBY3hSSHVTWCtwMVNXSFZabDJ1TjMvd1AzWWt1M1kwbXFkdzVPdk1Lc1htbkJFNGFtb3BvQm0KWFV4V2F6U3YxaG5iaGdMWkRWVHk4VmRYY0E9PQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg=="
proxy {
http = "http://proxy.com"
https = "https://proxy.com"
username = "user"
password = "pass"
}
}
`, agentId, accountId, agentName, namespace)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package agent_yaml

import (
"context"
"github.com/antihax/optional"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DataSourceGitopsAgentDeployYaml() *schema.Resource {
resource := &schema.Resource{
Description: "Datasource for fetching a Harness Gitops Agents.",

ReadContext: dataSourceGitopsAgentDeployYamlRead,

Schema: map[string]*schema.Schema{
"account_id": {
Description: "Account identifier of the GitOps agent.",
Type: schema.TypeString,
Required: true,
},
"project_id": {
Description: "Project identifier of the GitOps agent.",
Type: schema.TypeString,
Optional: true,
},
"org_id": {
Description: "Organization identifier of the GitOps agent.",
Type: schema.TypeString,
Optional: true,
},
"identifier": {
Description: "Identifier of the GitOps agent.",
Type: schema.TypeString,
Required: true,
},
"namespace": {
Description: "The k8s namespace that the GitOps agent resides in.",
Type: schema.TypeString,
Required: true,
},
"yaml": {
Description: "Deployment YAML of the GitOps agent.",
Type: schema.TypeString,
Computed: true,
},
},
}
return resource
}

func dataSourcePostDeployYaml(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
agentIdentifier := d.Get("identifier").(string)

resp, httpResp, err := c.AgentApi.AgentServiceForServerPostDeployYaml(ctx, agentIdentifier, c.AccountId, &nextgen.V1AgentYamlQuery{
OrgIdentifier: optional.NewString(d.Get("org_id").(string)),
ProjectIdentifier: optional.NewString(d.Get("project_id").(string)),
Namespace: optional.NewString(d.Get("namespace").(string)),
})

if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}

// Soft delete lookup error handling
// https://harness.atlassian.net/browse/PL-23765
if &resp == nil {
d.SetId("")
d.MarkNewResource()
return nil
}
readAgentYaml(agentIdentifier, d, resp)
return nil
}

func readAgentYaml(agentIdentifier string, d *schema.ResourceData, yaml string) {
d.SetId(agentIdentifier)
d.Set("yaml", yaml)
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func testAccDataSourceGitopsRepository(id string, name string, repo string, repo
update_mask {
paths = ["name"]
}
}
data "harness_platform_gitops_repository" "test" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func TestAccDataSourceGitOpsRepoCert(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "account_id", accountId),
resource.TestCheckResourceAttr(resourceName, "agent_id", agentId),
),
}, {
Config: testAccDataSourceRepoCertSSH(id, accountId, name, agentId, clusterName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "account_id", accountId),
resource.TestCheckResourceAttr(resourceName, "agent_id", agentId),
),
},
},
})
Expand Down Expand Up @@ -64,3 +70,38 @@ func testAccDataSourceRepoCert(id string, accountId string, name string, agentId
}
`, id, accountId, name, agentId, clusterName)
}

// bitbucket.org ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAubiN81eDcafrgMeLzaFPsw2kNvEcqTKl/VqLat/MaB33pZy0y3rJZtnqwR2qOOvbwKZYKiEO1O6VqNEBxKvJJelCq0dTXWT5pbO2gDXC6h6QDXCaHo6pOHGPUy+YBaGQRGuSusMEASYiWunYN0vCAI8QaXnWMXNMdFP3jHAJH0eDsoiGnLPBlBp4TNm6rYI74nMzgz3B9IikW4WVK+dc8KZJZWYjAuORU3jc1c/NPskD2ASinf8v3xnfXeukU0sJ5N6m5E8VLjObPEO+mN2t/FZTMZLiFqPWc/ALSqnMnnhwrNi2rbfg/rd/IpL8Le3pSBne8+seeFVBoGqzHM9yXw==
// "serverName":"bitbucket.org",
// "certType":"ssh",
// "certSubType": "ssh-rsa",
// "certData":"QUFBQUIzTnphQzF5YzJFQUFBQUJJd0FBQVFFQXViaU44MWVEY2FmcmdNZUx6YUZQc3cya052RWNxVEtsL1ZxTGF0L01hQjMzcFp5MHkzckpadG5xd1IycU9PdmJ3S1pZS2lFTzFPNlZxTkVCeEt2SkplbENxMGRUWFdUNXBiTzJnRFhDNmg2UURYQ2FIbzZwT0hHUFV5K1lCYUdRUkd1U3VzTUVBU1lpV3VuWU4wdkNBSThRYVhuV01YTk1kRlAzakhBSkgwZURzb2lHbkxQQmxCcDRUTm02cllJNzRuTXpnejNCOUlpa1c0V1ZLK2RjOEtaSlpXWWpBdU9SVTNqYzFjL05Qc2tEMkFTaW5mOHYzeG5mWGV1a1Uwc0o1TjZtNUU4VkxqT2JQRU8rbU4ydC9GWlRNWkxpRnFQV2MvQUxTcW5Nbm5od3JOaTJyYmZnL3JkL0lwTDhMZTNwU0JuZTgrc2VlRlZCb0dxekhNOXlYdz09","certInfo":""}]},"upsert":true}' \
func testAccDataSourceRepoCertSSH(id string, accountId string, name string, agentId string, clusterName string) string {
return fmt.Sprintf(`
resource "harness_platform_gitops_repo_cert" "test" {
account_id = "%[2]s"
agent_id = "%[4]s"
request {
upsert = true
certificates {
metadata {
}
items {
server_name = "bitbucket.org"
cert_type = "ssh"
cert_sub_type = "ssh-rsa"
cert_data = "QUFBQUIzTnphQzF5YzJFQUFBQUJJd0FBQVFFQXViaU44MWVEY2FmcmdNZUx6YUZQc3cya052RWNxVEtsL1ZxTGF0L01hQjMzcFp5MHkzckpadG5xd1IycU9PdmJ3S1pZS2lFTzFPNlZxTkVCeEt2SkplbENxMGRUWFdUNXBiTzJnRFhDNmg2UURYQ2FIbzZwT0hHUFV5K1lCYUdRUkd1U3VzTUVBU1lpV3VuWU4wdkNBSThRYVhuV01YTk1kRlAzakhBSkgwZURzb2lHbkxQQmxCcDRUTm02cllJNzRuTXpnejNCOUlpa1c0V1ZLK2RjOEtaSlpXWWpBdU9SVTNqYzFjL05Qc2tEMkFTaW5mOHYzeG5mWGV1a1Uwc0o1TjZtNUU4VkxqT2JQRU8rbU4ydC9GWlRNWkxpRnFQV2MvQUxTcW5Nbm5od3JOaTJyYmZnL3JkL0lwTDhMZTNwU0JuZTgrc2VlRlZCb0dxekhNOXlYdz09"
}
}
}
}
data "harness_platform_gitops_repo_cert" "test" {
depends_on = [harness_platform_gitops_repo_cert.test]
account_id = "%[2]s"
agent_id = "%[4]s"
}
`, id, accountId, name, agentId, clusterName)
}

0 comments on commit c8704e3

Please sign in to comment.