-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support of hosts management for server resource (#8)
Added support of hosts management for server resource Added datasource `pritunl_host`
- Loading branch information
Showing
12 changed files
with
438 additions
and
35 deletions.
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,35 @@ | ||
terraform { | ||
required_providers { | ||
pritunl = { | ||
version = "0.1.0" | ||
source = "disc/pritunl" | ||
} | ||
} | ||
} | ||
|
||
provider "pritunl" { | ||
url = var.pritunl_url | ||
token = var.pritunl_api_token | ||
secret = var.pritunl_api_secret | ||
|
||
insecure = var.pritunl_insecure | ||
} | ||
|
||
data "pritunl_host" "main" { | ||
hostname = "nyc1.vpn.host" | ||
} | ||
|
||
data "pritunl_host" "reserve" { | ||
hostname = "nyc3.vpn.host" | ||
} | ||
|
||
resource "pritunl_server" "test" { | ||
name = "some-server" | ||
network = "192.168.250.0/24" | ||
port = 15500 | ||
|
||
host_ids = [ | ||
data.pritunl_host.main.id, | ||
data.pritunl_host.reserve.id, | ||
] | ||
} |
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,19 @@ | ||
variable "pritunl_url" { | ||
type = string | ||
default = "http://localhost" | ||
} | ||
|
||
variable "pritunl_api_token" { | ||
type = string | ||
default = "secret" | ||
} | ||
|
||
variable "pritunl_api_secret" { | ||
type = string | ||
default = "secret" | ||
} | ||
|
||
variable "pritunl_insecure" { | ||
type = bool | ||
default = false | ||
} |
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,6 @@ | ||
package pritunl | ||
|
||
type Host struct { | ||
ID string `json:"id,omitempty"` | ||
Hostname string `json:"hostname"` | ||
} |
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,62 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/disc/terraform-provider-pritunl/internal/pritunl" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceHost() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "Use this data source to get information about the Pritunl hosts.", | ||
ReadContext: dataSourceHostsRead, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"hostname": { | ||
Description: "Hostname", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceHostsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
hostname := d.Get("hostname") | ||
filterFunction := func(host pritunl.Host) bool { | ||
return host.Hostname == hostname | ||
} | ||
|
||
host, err := filterHosts(meta, filterFunction) | ||
if err != nil { | ||
return diag.Errorf("could not a find host with a hostname %s. Previous error message: %v", hostname, err) | ||
} | ||
|
||
d.SetId(host.ID) | ||
d.Set("hostname", host.Hostname) | ||
|
||
return nil | ||
} | ||
|
||
func filterHosts(meta interface{}, test func(host pritunl.Host) bool) (pritunl.Host, error) { | ||
apiClient := meta.(pritunl.Client) | ||
|
||
hosts, err := apiClient.GetHosts() | ||
|
||
if err != nil { | ||
return pritunl.Host{}, err | ||
} | ||
|
||
for _, dir := range hosts { | ||
if test(dir) { | ||
return dir, nil | ||
} | ||
} | ||
|
||
return pritunl.Host{}, errors.New("could not find a host with specified parameters") | ||
} |
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,36 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func TestDataSourceHost(t *testing.T) { | ||
// pritunl.local sets in Makefile's "test" target | ||
existsHostname := "pritunl.local" | ||
notExistHostname := "not-exist-hostname" | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() {}, | ||
ProviderFactories: providerFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testdataHostSimpleConfig(existsHostname), | ||
Check: resource.ComposeTestCheckFunc(), | ||
}, | ||
{ | ||
Config: testdataHostSimpleConfig(notExistHostname), | ||
ExpectError: regexp.MustCompile(fmt.Sprintf("could not a find host with a hostname %s. Previous error message: could not find a host with specified parameters", notExistHostname)), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testdataHostSimpleConfig(name string) string { | ||
return fmt.Sprintf(` | ||
data "pritunl_host" "test" { | ||
hostname = "%[1]s" | ||
} | ||
`, name) | ||
} |
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
Oops, something went wrong.