Skip to content

Commit 27f8c8c

Browse files
author
iru
authored
feat: add datasource secure connection (#157)
* feature: add d secure connection * chore: add doc * doc: add links for tf plugin development
1 parent b7a59b4 commit 27f8c8c

File tree

8 files changed

+170
-3
lines changed

8 files changed

+170
-3
lines changed

.envrc.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# prod/kubelab
22
export SYSDIG_SECURE_API_TOKEN=
33
export SYSDIG_MONITOR_API_TOKEN=
4+
5+
# export SYSDIG_SECURE_URL=https://secure.sysdig.com

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*.exe
33
.DS_Store
44
.envrc
5+
.env
56
example.tf
67
terraform.tfplan
78
terraform.tfstate

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ Using the provider
3333
----------------------
3434
If you're building the provider, follow the instructions to [install it as a plugin.](https://www.terraform.io/docs/plugins/basics.html#installing-a-plugin) After placing it into your plugins directory, run `terraform init` to initialize it.
3535

36-
---
3736

3837
Contribute
3938
---------------------------
@@ -55,12 +54,29 @@ In order to test the provider, you can simply run `make test`.
5554
$ make test
5655
```
5756

58-
If you want to execute the acceptance tests, you can run `make testacc`. Please note that you need a token for Monitor and Secure, and since the acceptance tests create real infrastructure you should execute them in an environment where you can remove the resorces easily.
59-
57+
If you want to execute the acceptance tests, you can run `make testacc`.
6058
```sh
6159
$ make testacc
6260
```
6361

62+
<br/>:warning:Please note that you need a token for Monitor and Secure, and since the **acceptance tests create real infrastructure**
63+
you should execute them in an environment where you can remove the resorces easily.
64+
65+
66+
67+
### Creating new resource / data sources
68+
69+
TL;DR;
70+
- Create the resource/data source item
71+
- Add the created item into the `provider.go` resource or datasource map with its wiring
72+
- With its [acceptance test](https://www.terraform.io/plugin/sdkv2/testing/acceptance-tests)
73+
- Add its documentation page on `./website/docs/`
74+
75+
76+
https://www.terraform.io/plugin
77+
https://www.hashicorp.com/blog/testing-hashicorp-terraform
78+
79+
6480
### Proposing PR's
6581

6682
* on pull-requests some validations are enforced.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package sysdig
2+
3+
import (
4+
"context"
5+
"crypto/sha256"
6+
"fmt"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceSysdigSecureConnection() *schema.Resource {
12+
13+
return &schema.Resource{
14+
ReadContext: dataSourceSecureConnectionRead,
15+
Schema: map[string]*schema.Schema{
16+
"secure_url": {
17+
Type: schema.TypeString,
18+
Computed: true,
19+
Description: "Sysdig Secure URL basepath to where backend requests will be sent",
20+
},
21+
"secure_api_token": {
22+
Type: schema.TypeString,
23+
Computed: true,
24+
Sensitive: true,
25+
Description: "Sysdig Secure authentication api token",
26+
},
27+
},
28+
}
29+
}
30+
31+
func dataSourceSecureConnectionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
32+
33+
endpoint, err := meta.(SysdigClients).GetSecureEndpoint()
34+
if err != nil {
35+
return diag.FromErr(err)
36+
}
37+
38+
apiToken, err := meta.(SysdigClients).GetSecureApiToken()
39+
if err != nil {
40+
return diag.FromErr(err)
41+
}
42+
43+
d.SetId(fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprintf("%s,%s", endpoint, apiToken)))))
44+
45+
err = d.Set("secure_url", endpoint)
46+
if err != nil {
47+
return diag.FromErr(err)
48+
}
49+
50+
err = d.Set("secure_api_token", apiToken)
51+
if err != nil {
52+
return diag.FromErr(err)
53+
}
54+
return nil
55+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package sysdig_test
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
10+
"github.com/draios/terraform-provider-sysdig/sysdig"
11+
)
12+
13+
func TestAccSecureConnection(t *testing.T) {
14+
15+
dataSourceResourceName := "data.sysdig_secure_connection.current"
16+
17+
apiToken := os.Getenv("SYSDIG_SECURE_API_TOKEN")
18+
19+
resource.ParallelTest(t, resource.TestCase{
20+
21+
PreCheck: func() {
22+
if apiToken == "" {
23+
t.Fatal("SYSDIG_SECURE_API_TOKEN and must be set for acceptance tests")
24+
}
25+
},
26+
ProviderFactories: map[string]func() (*schema.Provider, error){
27+
"sysdig": func() (*schema.Provider, error) {
28+
return sysdig.Provider(), nil
29+
},
30+
},
31+
Steps: []resource.TestStep{
32+
{
33+
Config: getSysdigSecureCurrentConnection(),
34+
Check: resource.ComposeTestCheckFunc(
35+
resource.TestCheckResourceAttr(dataSourceResourceName, "secure_url", "https://secure.sysdig.com"),
36+
resource.TestCheckResourceAttr(dataSourceResourceName, "secure_api_token", apiToken),
37+
),
38+
},
39+
},
40+
})
41+
}
42+
43+
func getSysdigSecureCurrentConnection() string {
44+
return `
45+
data "sysdig_secure_connection" "current" {
46+
}
47+
`
48+
}

sysdig/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func Provider() *schema.Provider {
9494
"sysdig_secure_notification_channel": dataSourceSysdigSecureNotificationChannel(),
9595
"sysdig_current_user": dataSourceSysdigCurrentUser(),
9696
"sysdig_user": dataSourceSysdigUser(),
97+
"sysdig_secure_connection": dataSourceSysdigSecureConnection(),
9798

9899
"sysdig_fargate_workload_agent": dataSourceSysdigFargateWorkloadAgent(),
99100
"sysdig_monitor_notification_channel_pagerduty": dataSourceSysdigMonitorNotificationChannelPagerduty(),

sysdig/sysdig_clients.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import (
1212
)
1313

1414
type SysdigClients interface {
15+
GetSecureEndpoint() (string, error)
16+
GetSecureApiToken() (string, error)
17+
1518
sysdigMonitorClient() (monitor.SysdigMonitorClient, error)
1619
sysdigSecureClient() (secure.SysdigSecureClient, error)
1720
sysdigCommonClient() (common.SysdigCommonClient, error)
@@ -28,6 +31,22 @@ type sysdigClients struct {
2831
commonClient common.SysdigCommonClient
2932
}
3033

34+
func (c *sysdigClients) GetSecureEndpoint() (string, error) {
35+
endpoint := c.d.Get("sysdig_secure_url").(string)
36+
if endpoint == "" {
37+
return "", errors.New("GetSecureEndpoint, sysdig_secure_url not provided")
38+
}
39+
return endpoint, nil
40+
}
41+
42+
func (c *sysdigClients) GetSecureApiToken() (string, error) {
43+
secureAPIToken := c.d.Get("sysdig_secure_api_token").(string)
44+
if secureAPIToken == "" {
45+
return "", errors.New("GetSecureApiToken, sysdig secure token not provided")
46+
}
47+
return secureAPIToken, nil
48+
}
49+
3150
func (c *sysdigClients) sysdigMonitorClient() (m monitor.SysdigMonitorClient, err error) {
3251
c.mu.Lock()
3352
defer c.mu.Unlock()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
subcategory: "Sysdig Platform"
3+
layout: "sysdig"
4+
page_title: "Sysdig: sysdig_secure_connection"
5+
description: |-
6+
Provides secure connection details.
7+
---
8+
9+
# Data Source: sysdig_secure_connection
10+
11+
Provides information about current secure connection details.
12+
13+
## Example Usage
14+
15+
```terraform
16+
data "sysdig_secure_connection" "current" {
17+
}
18+
```
19+
20+
## Attributes Reference
21+
22+
The following attributes are exported:
23+
24+
* `secure_url` - Sysdig Secure Endpoint URL basepath.
25+
* `secure_api_token` - Sysdig Api Token for authentication (Sensitive).

0 commit comments

Comments
 (0)