Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beta orders #202

Merged
merged 3 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions ovh/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ovh
import (
"fmt"
"log"
"sync"
"time"

cleanhttp "github.com/hashicorp/go-cleanhttp"
Expand All @@ -16,6 +17,9 @@ type Config struct {
ApplicationSecret string
ConsumerKey string
OVHClient *ovh.Client
authenticated bool
authFailed error
lockAuth *sync.Mutex
}

type OvhAuthCurrentCredential struct {
Expand Down Expand Up @@ -43,6 +47,31 @@ func clientDefault(c *Config) (*ovh.Client, error) {
}

func (c *Config) loadAndValidate() error {
if err := c.load(); err != nil {
return err
}

c.lockAuth.Lock()
defer c.lockAuth.Unlock()

if c.authFailed != nil {
return c.authFailed
}

if !c.authenticated {
var cred OvhAuthCurrentCredential
if err := c.OVHClient.Get("/auth/currentCredential", &cred); err != nil {
c.authFailed = fmt.Errorf("OVH client seems to be misconfigured: %q\n", err)
return c.authFailed
}

log.Printf("[DEBUG] Logged in on OVH API")
c.authenticated = true
}
return nil
}

func (c *Config) load() error {
validEndpoint := false

ovhEndpoints := [7]string{ovh.OvhEU, ovh.OvhCA, ovh.OvhUS, ovh.KimsufiEU, ovh.KimsufiCA, ovh.SoyoustartEU, ovh.SoyoustartCA}
Expand All @@ -69,14 +98,6 @@ func (c *Config) loadAndValidate() error {
}

httpClient.Transport = logging.NewTransport("OVH", httpClient.Transport)

var cred OvhAuthCurrentCredential
err = targetClient.Get("/auth/currentCredential", &cred)
if err != nil {
return fmt.Errorf("OVH client seems to be misconfigured: %q\n", err)
}

log.Printf("[DEBUG] Logged in on OVH API")
c.OVHClient = targetClient

return nil
Expand Down
97 changes: 97 additions & 0 deletions ovh/data_dbaas_logs_input_engine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package ovh

import (
"fmt"
"log"
"net/url"
"strings"

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

func dataSourceDbaasLogsInputEngine() *schema.Resource {
return &schema.Resource{
Read: func(d *schema.ResourceData, meta interface{}) error {
return dataSourceDbaasLogsInputEngineRead(d, meta)
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"is_deprecated": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"version": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}

func dataSourceDbaasLogsInputEngineRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

log.Printf("[DEBUG] Will read dbaas logs input engines")
res := []string{}
endpoint := fmt.Sprintf("/dbaas/logs/input/engine")
if err := config.OVHClient.Get(endpoint, &res); err != nil {
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
}

nameFilter := helpers.GetNilStringPointerFromData(d, "name")
isDeprecatedFilter := helpers.GetNilBoolPointerFromData(d, "is_deprecated")
versionFilter := helpers.GetNilStringPointerFromData(d, "version")

engines := []*DbaasLogsInputEngine{}

for _, id := range res {
log.Printf("[DEBUG] Will read dbaas logs input engine %s", id)
endpoint := fmt.Sprintf("/dbaas/logs/input/engine/%s",
url.PathEscape(id),
)
engine := &DbaasLogsInputEngine{}
if err := config.OVHClient.Get(endpoint, &engine); err != nil {
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
}

match := true
if nameFilter != nil && strings.ToLower(engine.Name) != strings.ToLower(*nameFilter) {
match = false
}
if versionFilter != nil && strings.ToLower(engine.Version) != strings.ToLower(*versionFilter) {
match = false
}
if isDeprecatedFilter != nil && engine.IsDeprecated != *isDeprecatedFilter {
match = false
}
if match {
engines = append(engines, engine)
}
}

if len(engines) == 0 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
if len(engines) > 1 {
return fmt.Errorf("Your query returned more than one result. " +
"Please change your search criteria and try again.")
}

for k, v := range engines[0].ToMap() {
if k != "id" {
d.Set(k, v)
} else {
d.SetId(fmt.Sprint(v))
}
}

return nil
}
71 changes: 71 additions & 0 deletions ovh/data_dbaas_logs_input_engine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ovh

import (
"testing"

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

const testAccDataSourceDbaasLogsInputEngine_deprecated = `
data "ovh_dbaas_logs_input_engine" "logstash" {
name = "logstash"
version = "6.8"
is_deprecated = true
}
`
const testAccDataSourceDbaasLogsInputEngine_basic = `
data "ovh_dbaas_logs_input_engine" "logstash" {
name = "logstash"
version = "7.x"
}
`

func TestAccDataSourceDbaasLogsInputEngine_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCredentials(t) },

Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceDbaasLogsInputEngine_basic,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.ovh_dbaas_logs_input_engine.logstash",
"is_deprecated",
"false",
),
resource.TestCheckResourceAttr(
"data.ovh_dbaas_logs_input_engine.logstash",
"version",
"7.x",
),
),
},
},
})
}

func TestAccDataSourceDbaasLogsInputEngine_deprecated(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCredentials(t) },

Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceDbaasLogsInputEngine_deprecated,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.ovh_dbaas_logs_input_engine.logstash",
"is_deprecated",
"true",
),
resource.TestCheckResourceAttr(
"data.ovh_dbaas_logs_input_engine.logstash",
"version",
"6.8",
),
),
},
},
})
}
Loading