Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #22 from OpenVPN/update-user
Browse files Browse the repository at this point in the history
Update user, add CI
  • Loading branch information
vladimir-kozyrev authored Mar 13, 2023
2 parents 480069b + 022f995 commit ad282e7
Show file tree
Hide file tree
Showing 16 changed files with 366 additions and 47 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
name: golangci-lint
on:
push:
branches:
- main
pull_request:
branches: [ main ]

jobs:
lint:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v3
with:
go-version-file: "go.mod"
cache: true

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version-file: "go.mod"
cache: true

- name: Import GPG key
id: import_gpg
Expand Down
73 changes: 73 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Tests
on:
pull_request:
branches:
- main
paths-ignore:
- "README.md"
push:
branches:
- main
paths-ignore:
- "README.md"
# We test at a regular interval to ensure we are alerted to something breaking due
# to an API change, even if the code did not change.
schedule:
- cron: "0 0 * * *"
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
# ensure the code builds...
build:
name: Build
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version-file: "go.mod"
cache: true
- name: Get dependencies
run: |
go mod download
- name: Build
run: |
go build -v .
# run acceptance tests in a matrix with Terraform core versions
test:
name: Matrix Test
needs: build
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
fail-fast: false
matrix:
terraform:
- "1.2.*"
- "1.3.*"
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version-file: "go.mod"
cache: true

- uses: hashicorp/setup-terraform@v2
with:
terraform_version: ${{ matrix.terraform }}
terraform_wrapper: false

- name: Get dependencies
run: |
go mod download
- name: TF acceptance tests
timeout-minutes: 10
env:
TF_ACC: "1"
OPENVPNCLOUD_TEST_ORGANIZATION: "terraform-community"
OPENVPN_CLOUD_CLIENT_ID: ${{ secrets.CVPN_CLIENT_ID }}
OPENVPN_CLOUD_CLIENT_SECRET: ${{ secrets.CVPN_CLIENT_SECRET }}
run: |
go test -v -cover ./openvpncloud
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
issues:
exclude-rules:
- linters:
- errcheck
text: "Error return value of `d.Set` is not checked"
6 changes: 3 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/json"
"fmt"
"golang.org/x/time/rate"
"io/ioutil"
"io"
"net/http"
"time"
)
Expand Down Expand Up @@ -39,7 +39,7 @@ func NewClient(baseUrl, clientId, clientSecret string) (*Client, error) {
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -74,7 +74,7 @@ func (c *Client) DoRequest(req *http.Request) ([]byte, error) {
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions client/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ func (c *Client) AddConnector(connector Connector, networkItemId string) (*Conne
return nil, err
}
body, err := c.DoRequest(req)
if err != nil {
return nil, err
}
var conn Connector
err = json.Unmarshal(body, &conn)
if err != nil {
Expand Down
37 changes: 27 additions & 10 deletions client/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import (
)

type User struct {
Id string `json:"id"`
Username string `json:"username"`
Role string `json:"role"`
Email string `json:"email"`
AuthType string `json:"authType"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
GroupId string `json:"groupId"`
Status string `json:"status"`
Devices []Device `json:"devices"`
Id string `json:"id"`
Username string `json:"username"`
Role string `json:"role"`
Email string `json:"email"`
AuthType string `json:"authType"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
GroupId string `json:"groupId"`
Status string `json:"status"`
AccountStatus string `json:"accountStatus"`
Devices []Device `json:"devices"`
}

type Device struct {
Expand Down Expand Up @@ -88,6 +89,22 @@ func (c *Client) GetUserById(userId string) (*User, error) {
return &u, nil
}

func (c *Client) UpdateUser(user User) error {
userJson, err := json.Marshal(user)
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("%s/api/beta/users/%s", c.BaseURL, user.Id), bytes.NewBuffer(userJson))
if err != nil {
return err
}
_, err = c.DoRequest(req)
if err != nil {
return err
}
return nil
}

func (c *Client) DeleteUser(userId string) error {
req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/api/beta/users/%s", c.BaseURL, userId), nil)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion client/user_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ func (c *Client) GetUserGroup(name string) (*UserGroup, error) {
return &ug, nil
}
}
return nil, nil
return nil, fmt.Errorf("group %s does not exist", name)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
github.com/gruntwork-io/terratest v0.41.6
github.com/hashicorp/terraform-plugin-sdk/v2 v2.25.0
github.com/stretchr/testify v1.7.2
github.com/stretchr/testify v1.8.1
golang.org/x/time v0.3.0
)

Expand Down
7 changes: 6 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,19 @@ github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkU
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tmccombs/hcl2json v0.3.3 h1:+DLNYqpWE0CsOQiEZu+OZm5ZBImake3wtITYxQ8uLFQ=
github.com/tmccombs/hcl2json v0.3.3/go.mod h1:Y2chtz2x9bAeRTvSibVRVgbLJhLJXKlUeIvjeVdnm4w=
github.com/ulikunitz/xz v0.5.8 h1:ERv8V6GKqVi23rgu5cj9pVfVzJbOqAY2Ntl88O6c2nQ=
Expand Down
4 changes: 2 additions & 2 deletions openvpncloud/data_source_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func dataSourceNetworkRead(ctx context.Context, d *schema.ResourceData, m interf
}

func getRoutesSlice(networkRoutes *[]client.Route) []interface{} {
routes := make([]interface{}, len(*networkRoutes), len(*networkRoutes))
routes := make([]interface{}, len(*networkRoutes))
for i, r := range *networkRoutes {
route := make(map[string]interface{})
route["id"] = r.Id
Expand All @@ -151,7 +151,7 @@ func getRoutesSlice(networkRoutes *[]client.Route) []interface{} {
}

func getConnectorsSlice(connectors *[]client.Connector) []interface{} {
conns := make([]interface{}, len(*connectors), len(*connectors))
conns := make([]interface{}, len(*connectors))
for i, c := range *connectors {
connector := make(map[string]interface{})
connector["id"] = c.Id
Expand Down
2 changes: 1 addition & 1 deletion openvpncloud/data_source_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func dataSourceUserRead(ctx context.Context, d *schema.ResourceData, m interface
}

func getUserDevicesSlice(userDevices *[]client.Device) []interface{} {
devices := make([]interface{}, len(*userDevices), len(*userDevices))
devices := make([]interface{}, len(*userDevices))
for i, d := range *userDevices {
device := make(map[string]interface{})
device["id"] = d.Id
Expand Down
42 changes: 42 additions & 0 deletions openvpncloud/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package openvpncloud

import (
"os"
"testing"

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

const alphabet = "abcdefghigklmnopqrstuvwxyz"

var testCloudID = os.Getenv("OPENVPNCLOUD_TEST_ORGANIZATION")
var testAccProvider *schema.Provider
var testAccProviders map[string]*schema.Provider
var testAccProviderFactories map[string]func() (*schema.Provider, error)

func init() {
testAccProvider = Provider()
testAccProviders = map[string]*schema.Provider{
"openvpncloud": testAccProvider,
}
testAccProviderFactories = map[string]func() (*schema.Provider, error){
"openvpncloud": func() (*schema.Provider, error) {
return testAccProvider, nil
},
}
}

func TestProvider(t *testing.T) {
if err := Provider().InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("OPENVPN_CLOUD_CLIENT_ID"); v == "" {
t.Fatal("OPENVPN_CLOUD_CLIENT_ID must be set for acceptance tests")
}
if v := os.Getenv("OPENVPN_CLOUD_CLIENT_SECRET"); v == "" {
t.Fatal("OPENVPN_CLOUD_CLIENT_SECRET must be set for acceptance tests")
}
}
22 changes: 0 additions & 22 deletions openvpncloud/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,25 +347,3 @@ func resourceNetworkDelete(ctx context.Context, d *schema.ResourceData, m interf
}
return diags
}

func getNetworkConnectorSlice(networkConnectors []client.Connector, networkId string, connectorName string) []interface{} {
if len(networkConnectors) == 0 {
return nil
}
connectorsList := make([]interface{}, 1)
for _, c := range networkConnectors {
if c.NetworkItemId == networkId && c.Name == connectorName {
connector := make(map[string]interface{})
connector["id"] = c.Id
connector["name"] = c.Name
connector["network_item_id"] = c.NetworkItemId
connector["network_item_type"] = c.NetworkItemType
connector["vpn_region_id"] = c.VpnRegionId
connector["ip_v4_address"] = c.IPv4Address
connector["ip_v6_address"] = c.IPv6Address
connectorsList[0] = connector
break
}
}
return connectorsList
}
Loading

0 comments on commit ad282e7

Please sign in to comment.