generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for datasource delegate
- Loading branch information
1 parent
0b89589
commit 188da3f
Showing
5 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters