Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Sep 1, 2022
1 parent cf7c228 commit 496de7f
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 213 deletions.
20 changes: 11 additions & 9 deletions docs/content/dns/zz_gen_vkcloud.md
Expand Up @@ -26,9 +26,9 @@ Configuration for [VK Cloud](https://mcs.mail.ru/).
Here is an example bash command using the VK Cloud provider:

```bash
VK_CLOUD_PROJECT_ID="<your project id string>" \
VK_CLOUD_USERNAME="<your email>" \
VK_CLOUD_PASSWORD="<your password>" \
VK_CLOUD_PROJECT_ID="<your_project_id>" \
VK_CLOUD_USERNAME="<your_email>" \
VK_CLOUD_PASSWORD="<your_password>" \
lego --email you@example.com --dns vkcloud --domains "example.org" --domains "*.example.org" run
```

Expand Down Expand Up @@ -62,13 +62,15 @@ The environment variable names can be suffixed by `_FILE` to reference a file in
More information [here]({{< ref "dns#configuration-and-credentials" >}}).

## Credential inforamtion
You can find all required and additional information on ["Project/Keys" page](https://mcs.mail.ru/app/en/project/keys) of your cloud

| ENV Variable | Parameter from page |
| VK_CLOUD_PROJECT_ID | Project ID |
| VK_CLOUD_USERNAME | Username |
| VK_CLOUD_DOMAIN_NAME | User Domain Name |
| VK_CLOUD_IDENTITY_ENDPOINT | User Domain Name |
You can find all required and additional information on ["Project/Keys" page](https://mcs.mail.ru/app/en/project/keys) of your cloud.

| ENV Variable | Parameter from page |
|----------------------------|---------------------|
| VK_CLOUD_PROJECT_ID | Project ID |
| VK_CLOUD_USERNAME | Username |
| VK_CLOUD_DOMAIN_NAME | User Domain Name |
| VK_CLOUD_IDENTITY_ENDPOINT | Identity endpoint |



Expand Down
123 changes: 0 additions & 123 deletions providers/dns/vkcloud/client.go

This file was deleted.

138 changes: 138 additions & 0 deletions providers/dns/vkcloud/internal/client.go
@@ -0,0 +1,138 @@
package internal

import (
"errors"
"fmt"
"net/http"
"net/url"
"path"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
)

// Client VK client.
type Client struct {
baseURL *url.URL
openstack *gophercloud.ProviderClient
authOpts gophercloud.AuthOptions
authenticated bool
}

// NewClient creates a Client.
func NewClient(endpoint string, authOpts gophercloud.AuthOptions) (*Client, error) {
err := validateAuthOptions(authOpts)
if err != nil {
return nil, err
}

openstackClient, err := openstack.NewClient(authOpts.IdentityEndpoint)
if err != nil {
return nil, fmt.Errorf("new client: %w", err)
}

baseURL, err := url.Parse(endpoint)
if err != nil {
return nil, fmt.Errorf("parse URL: %w", err)
}

return &Client{
baseURL: baseURL,
openstack: openstackClient,
authOpts: authOpts,
}, nil
}

func (c *Client) ListZones() ([]DNSZone, error) {
var zones []DNSZone
opts := &gophercloud.RequestOpts{JSONResponse: &zones}

err := c.request(http.MethodGet, "", opts)
if err != nil {
return nil, err
}

return zones, nil
}

func (c *Client) ListTXTRecords(zoneUUID string) ([]DNSTXTRecord, error) {
var records []DNSTXTRecord
opts := &gophercloud.RequestOpts{JSONResponse: &records}

err := c.request(http.MethodGet, path.Join(zoneUUID, "txt"), opts)
if err != nil {
return nil, err
}

return records, nil
}

func (c *Client) CreateTXTRecord(zoneUUID string, record *DNSTXTRecord) error {
opts := &gophercloud.RequestOpts{
JSONBody: record,
JSONResponse: record,
}

return c.request(http.MethodPost, path.Join(zoneUUID, "txt"), opts)
}

func (c *Client) DeleteTXTRecord(zoneUUID, recordUUID string) error {
return c.request(http.MethodDelete, path.Join(zoneUUID, "txt", recordUUID), &gophercloud.RequestOpts{})
}

func (c *Client) request(method, uri string, options *gophercloud.RequestOpts) error {
if err := c.lazyAuth(); err != nil {
return fmt.Errorf("auth: %w", err)
}

endpoint, err := c.baseURL.Parse(path.Join(c.baseURL.Path, "v2", "dns", uri))
if err != nil {
return err
}

_, err = c.openstack.Request(method, endpoint.String(), options)
if err != nil {
return fmt.Errorf("request: %w", err)
}

return nil
}

func (c *Client) lazyAuth() error {
if c.authenticated {
return nil
}

err := openstack.Authenticate(c.openstack, c.authOpts)
if err != nil {
return err
}

c.authenticated = true

return nil
}

func validateAuthOptions(opts gophercloud.AuthOptions) error {
if opts.TenantID == "" {
return errors.New("project id is missing in credentials information")
}

if opts.Username == "" {
return errors.New("username is missing in credentials information")
}

if opts.Password == "" {
return errors.New("password is missing in credentials information")
}

if opts.IdentityEndpoint == "" {
return errors.New("identity endpoint is missing in config")
}

if opts.DomainName == "" {
return errors.New("domain name is missing in config")
}

return nil
}
@@ -1,4 +1,4 @@
package vkcloud
package internal

type DNSZone struct {
UUID string `json:"uuid,omitempty"`
Expand All @@ -21,7 +21,3 @@ type DNSTXTRecord struct {
Content string `json:"content,omitempty"`
TTL int `json:"ttl,omitempty"`
}

type DNSZones = []DNSZone

type DNSTXTRecords = []DNSTXTRecord

0 comments on commit 496de7f

Please sign in to comment.