Skip to content

Commit

Permalink
Added support for datasource delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
micahlmartin committed Oct 8, 2021
1 parent 0b89589 commit 188da3f
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/aws/aws-sdk-go v1.37.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/harness-io/harness-go-sdk v0.0.11
github.com/harness-io/harness-go-sdk v0.0.12
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-hclog v0.16.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ github.com/harness-io/harness-go-sdk v0.0.10 h1:QDUw5LFJP9MO6Gr6NIPWbD0rGWX+4qVl
github.com/harness-io/harness-go-sdk v0.0.10/go.mod h1:/5oCswBe0pg/6GAuviqJErMxGgQWx+/OOD7UQ74d1FY=
github.com/harness-io/harness-go-sdk v0.0.11 h1:qfIYLlPgfAzvQJx/+ZbjFneuQfN0hzfQuVBLph976AA=
github.com/harness-io/harness-go-sdk v0.0.11/go.mod h1:/5oCswBe0pg/6GAuviqJErMxGgQWx+/OOD7UQ74d1FY=
github.com/harness-io/harness-go-sdk v0.0.12 h1:EIy3PNsiSCrykctPE3PzNo7IZh5ubyoMY77ppQMREAA=
github.com/harness-io/harness-go-sdk v0.0.12/go.mod h1:/5oCswBe0pg/6GAuviqJErMxGgQWx+/OOD7UQ74d1FY=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down
118 changes: 118 additions & 0 deletions internal/provider/data_source_delegate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package provider

import (
"context"
"fmt"
"strings"

"github.com/harness-io/harness-go-sdk/harness/api"
"github.com/harness-io/harness-go-sdk/harness/api/graphql"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceDelegate() *schema.Resource {
return &schema.Resource{
Description: "Data source for retrieving a Harness delegate. If more than one delegate matches the query the first one will be returned.",

ReadContext: dataSourceDelegateRead,

Schema: map[string]*schema.Schema{
"id": {
Description: "Unique identifier of the delegate",
Type: schema.TypeString,
Computed: true,
},
"name": {
Description: "The name of the delegate to query for.",
Type: schema.TypeString,
Optional: true,
},
"status": {
Description: fmt.Sprintf("The status of the delegate to query for. Valid values are %s", strings.Join(graphql.DelegateStatusSlice, ", ")),
Type: schema.TypeString,
Optional: true,
},
"type": {
Description: fmt.Sprintf("The type of the delegate to query for. Valid values are %s", strings.Join(graphql.DelegateTypesSlice, ", ")),
Type: schema.TypeString,
Optional: true,
},
"account_id": {
Description: "The account id the delegate belongs to.",
Type: schema.TypeString,
Computed: true,
},
"profile_id": {
Description: "The id of the profile assigned to the delegate.",
Type: schema.TypeString,
Computed: true,
},
"description": {
Description: "The description of the delegate.",
Type: schema.TypeString,
Computed: true,
},
"host_name": {
Description: "The host name of the delegate.",
Type: schema.TypeString,
Computed: true,
},
"ip": {
Description: "The ip address of the delegate.",
Type: schema.TypeString,
Computed: true,
},
"last_heartbeat": {
Description: "The last time the delegate was heard from.",
Type: schema.TypeString,
Computed: true,
},
"polling_mode_enabled": {
Description: "Whether the delegate is in polling mode.",
Type: schema.TypeBool,
Computed: true,
},
"version": {
Description: "The version of the delegate.",
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceDelegateRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// use the meta value to retrieve your client from the provider configure method
c := meta.(*api.Client)

name := d.Get("name").(string)
status := d.Get("status").(string)
delegateType := d.Get("type").(string)

delegates, _, err := c.Delegates().GetDelegateWithFilters(1, 0, name, graphql.DelegateStatus(status), graphql.DelegateType(delegateType))

if err != nil {
return diag.FromErr(err)
}

if len(delegates) == 0 {
return diag.Errorf("no delegate found with name %s, status %s, type %s", name, status, delegateType)
}

delegate := delegates[0]

d.SetId(delegate.UUID)
d.Set("name", delegate.DelegateName)
d.Set("status", delegate.Status)
d.Set("type", delegate.DelegateType)
d.Set("account_id", delegate.AccountId)
d.Set("profile_id", delegate.DelegateProfileId)
d.Set("description", delegate.Description)
d.Set("host_name", delegate.HostName)
d.Set("ip", delegate.Ip)
d.Set("last_heartbeat", delegate.LastHeartBeat)
d.Set("version", delegate.Version)

return nil
}
45 changes: 45 additions & 0 deletions internal/provider/data_source_delegate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package provider

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceDelegate(t *testing.T) {

var (
resourceName = "data.harness_delegate.test"
)

resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceDelegate(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", "harness-delegate"),
resource.TestCheckResourceAttr(resourceName, "ip", "10.12.1.8"),
resource.TestCheckResourceAttr(resourceName, "host_name", "harness-delegate-ukhyts-1"),
resource.TestCheckResourceAttr(resourceName, "id", "ZAgU6QnGSAa1vbsZLgdqcQ"),
resource.TestCheckResourceAttr(resourceName, "account_id", "UKh5Yts7THSMAbccG3HrLA"),
resource.TestCheckResourceAttr(resourceName, "profile_id", "wVhjS1xATpqGrO-uuRrYEw"),
resource.TestCheckResourceAttr(resourceName, "type", "KUBERNETES"),
resource.TestCheckResourceAttr(resourceName, "description", ""),
resource.TestCheckResourceAttrSet(resourceName, "last_heartbeat"),
resource.TestCheckResourceAttr(resourceName, "status", "ENABLED"),
resource.TestCheckResourceAttr(resourceName, "version", ""),
),
},
},
})
}

func testAccDataSourceDelegate() string {
return `
data "harness_delegate" "test" {
name = "harness-delegate"
}
`
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func New(version string) func() *schema.Provider {
},
DataSourcesMap: map[string]*schema.Resource{
"harness_application": dataSourceApplication(),
"harness_delegate": dataSourceDelegate(),
"harness_secret_manager": dataSourceSecretManager(),
"harness_encrypted_text": dataSourceEncryptedText(),
"harness_git_connector": dataSourceGitConnector(),
Expand Down

0 comments on commit 188da3f

Please sign in to comment.