From 3928529153566280cab93da0844359fdf9e7018e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Duchaussois?= Date: Thu, 3 May 2018 12:11:10 +0200 Subject: [PATCH 1/4] add ovh_credit_card data source --- ovh/data_source_ovh_creditcard.go | 176 ++++++++++++++++++++++++++++++ ovh/provider.go | 1 + 2 files changed, 177 insertions(+) create mode 100644 ovh/data_source_ovh_creditcard.go diff --git a/ovh/data_source_ovh_creditcard.go b/ovh/data_source_ovh_creditcard.go new file mode 100644 index 000000000..b1958f0fd --- /dev/null +++ b/ovh/data_source_ovh_creditcard.go @@ -0,0 +1,176 @@ +package ovh + +import ( + "fmt" + "regexp" + "sort" + + "github.com/hashicorp/terraform/helper/schema" +) + +type CreditCard struct { + Description string `json:"description"` + Number string `json:"number"` + Expiration string `json:"expirationDate"` + Default bool `json:"defaultPaymentMean"` + State string `json:"state"` + ThreeDSValidated bool `json:"threeDsValidated"` + Id int `json:"id"` + Type string `json:"type"` +} + +func dataSourceCreditCard() *schema.Resource { + return &schema.Resource{ + Read: dataSourceCreditCardRead, + Schema: map[string]*schema.Schema{ + "description_regexp": &schema.Schema{ + Type: schema.TypeString, + ForceNew: true, + Optional: true, + Default: ".*", + }, + "use_default": &schema.Schema{ + Type: schema.TypeBool, + ForceNew: true, + Optional: true, + Default: false, + }, + "use_last_to_expire": &schema.Schema{ + Type: schema.TypeBool, + ForceNew: true, + Optional: true, + Default: false, + }, + "states": &schema.Schema{ + Type: schema.TypeSet, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + Optional: true, + }, + // Computed + "description": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "number": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "expiration_date": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "type": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "state": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "id": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + }, + "default": &schema.Schema{ + Type: schema.TypeBool, + Computed: true, + }, + "threeds_validated": &schema.Schema{ + Type: schema.TypeBool, + Computed: true, + }, + }, + } +} + +func dataSourceCreditCardRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + states_val, states_ok := d.GetOk("states") + description_regexp := regexp.MustCompile(d.Get("description_regexp").(string)) + use_last_to_expire := d.Get("use_last_to_expire").(bool) + use_default := d.Get("use_default").(bool) + var the_credit_card *CreditCard + var states []interface{} + if states_ok { + states = states_val.(*schema.Set).List() + } + var credit_card_ids []int + err := config.OVHClient.Get( + "/me/paymentMean/creditCard", + &credit_card_ids, + ) + + if err != nil { + return fmt.Errorf("Error getting Credit Cards list:\n\t %q", err) + } + filtered_credit_cards := []*CreditCard{} + for _, card_id := range credit_card_ids { + credit_card := CreditCard{} + err = config.OVHClient.Get( + fmt.Sprintf("/me/paymentMean/creditCard/%d", card_id), + &credit_card, + ) + if err != nil { + return fmt.Errorf("Error getting Credit Card %d:\n\t %q", card_id, err) + } + if use_default && credit_card.Default == false { + continue + } + if states_ok { + match := false + for _, wanted_state := range states { + if credit_card.State == wanted_state { + match = true + break + } + } + if !match { + continue + } + } + if !description_regexp.MatchString(credit_card.Description) { + continue + } + filtered_credit_cards = append(filtered_credit_cards, &credit_card) + } + if len(filtered_credit_cards) < 1 { + return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.") + } + if len(filtered_credit_cards) > 1 { + if use_last_to_expire { + sort.Slice(filtered_credit_cards, func(i, j int) bool { + return (*filtered_credit_cards[i]).Expiration > (*filtered_credit_cards[j]).Expiration + }) + the_credit_card = filtered_credit_cards[0] + } + if use_default { + match := false + for _, credit_card := range filtered_credit_cards { + if (*credit_card).Default { + match = true + the_credit_card = credit_card + break + } + } + if match == false { + return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.") + } + } + } + if len(filtered_credit_cards) == 1 { + the_credit_card = filtered_credit_cards[0] + } + // Set data + d.Set("description", (*the_credit_card).Description) + d.Set("number", (*the_credit_card).Number) + d.Set("expiration_date", (*the_credit_card).Expiration) + d.Set("type", (*the_credit_card).Type) + d.Set("state", (*the_credit_card).State) + d.Set("id", (*the_credit_card).Id) + d.Set("default", (*the_credit_card).Default) + d.Set("threeds_validated", (*the_credit_card).ThreeDSValidated) + d.SetId(fmt.Sprintf("%d", (*the_credit_card).Id)) + return nil +} diff --git a/ovh/provider.go b/ovh/provider.go index 3de5e6a77..81f59fa04 100644 --- a/ovh/provider.go +++ b/ovh/provider.go @@ -49,6 +49,7 @@ func Provider() terraform.ResourceProvider { // Legacy naming schema (new datasources should not be added here) "ovh_publiccloud_region": dataSourcePublicCloudRegion(), "ovh_publiccloud_regions": dataSourcePublicCloudRegions(), + "ovh_credit_card": dataSourceCreditCard(), }, ResourcesMap: map[string]*schema.Resource{ From f596d03f3be48bbb78e3da81f550f35549998025 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Duchaussois?= Date: Thu, 3 May 2018 12:17:59 +0200 Subject: [PATCH 2/4] add ovh_bank_account data source --- ovh/data_source_ovh_bankaccount.go | 183 +++++++++++++++++++++++++++++ ovh/provider.go | 1 + 2 files changed, 184 insertions(+) create mode 100644 ovh/data_source_ovh_bankaccount.go diff --git a/ovh/data_source_ovh_bankaccount.go b/ovh/data_source_ovh_bankaccount.go new file mode 100644 index 000000000..2a0cde85f --- /dev/null +++ b/ovh/data_source_ovh_bankaccount.go @@ -0,0 +1,183 @@ +package ovh + +import ( + "fmt" + "regexp" + "sort" + + "github.com/hashicorp/terraform/helper/schema" +) + +type BankAccount struct { + Description string `json:"description"` + Default bool `json:"defaultPaymentMean"` + State string `json:"state"` + Id int `json:"id"` + ValidationDocumentLink string `json:"validationDocumentLink"` + UniqueReference string `json:"uniqueReference"` + CreationDate string `json:"creationDate"` + MandateSignatureDate string `json:"mandateSignatureDate"` + OwnerName string `json:"ownerName"` + OwnerAddress string `json:"ownerAddress"` + Iban string `json:"iban"` + Bic string `json:"bic"` +} + +func dataSourceBankAccount() *schema.Resource { + return &schema.Resource{ + Read: dataSourceBankAccountRead, + Schema: map[string]*schema.Schema{ + "description_regexp": &schema.Schema{ + Type: schema.TypeString, + ForceNew: true, + Optional: true, + Default: ".*", + }, + "use_default": &schema.Schema{ + Type: schema.TypeBool, + ForceNew: true, + Optional: true, + Default: false, + }, + "use_oldest": &schema.Schema{ + Type: schema.TypeBool, + ForceNew: true, + Optional: true, + Default: false, + }, + "state": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + // Computed + "description": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "validation_document_link": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "unique_reference": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "creation_date": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "mandate_signature_date": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "owner_name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "owner_address": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "iban": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "bic": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "id": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + }, + "default": &schema.Schema{ + Type: schema.TypeBool, + Computed: true, + }, + }, + } +} + +func dataSourceBankAccountRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + state, state_ok := d.GetOk("state") + description_regexp := regexp.MustCompile(d.Get("description_regexp").(string)) + use_oldest := d.Get("use_oldest").(bool) + use_default := d.Get("use_default").(bool) + var the_bank_account *BankAccount + var bank_account_ids []int + endpoint := "/me/paymentMean/bankAccount" + if state_ok { + endpoint = fmt.Sprintf("%s?state=%s", endpoint, state) + } + err := config.OVHClient.Get( + endpoint, + &bank_account_ids, + ) + + if err != nil { + return fmt.Errorf("Error getting Bank Account list:\n\t %q", err) + } + filtered_bank_accounts := []*BankAccount{} + for _, account_id := range bank_account_ids { + bank_account := BankAccount{} + err = config.OVHClient.Get( + fmt.Sprintf("/me/paymentMean/bankAccount/%d", account_id), + &bank_account, + ) + if err != nil { + return fmt.Errorf("Error getting Bank Account %d:\n\t %q", account_id, err) + } + if use_default && bank_account.Default == false { + continue + } + if !description_regexp.MatchString(bank_account.Description) { + continue + } + filtered_bank_accounts = append(filtered_bank_accounts, &bank_account) + } + if len(filtered_bank_accounts) < 1 { + return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.") + } + if len(filtered_bank_accounts) > 1 { + if use_oldest { + sort.Slice(filtered_bank_accounts, func(i, j int) bool { + return (*filtered_bank_accounts[i]).CreationDate < (*filtered_bank_accounts[j]).CreationDate + }) + the_bank_account = filtered_bank_accounts[0] + } + if use_default { + match := false + for _, bank_account := range filtered_bank_accounts { + if (*bank_account).Default { + match = true + the_bank_account = bank_account + break + } + } + if match == false { + return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.") + } + } + } + if len(filtered_bank_accounts) == 1 { + the_bank_account = filtered_bank_accounts[0] + } + // Set data + d.Set("description", (*the_bank_account).Description) + d.Set("state", (*the_bank_account).State) + d.Set("id", (*the_bank_account).Id) + d.Set("default", (*the_bank_account).Default) + d.Set("validation_document_link", (*the_bank_account).ValidationDocumentLink) + d.Set("unique_reference", (*the_bank_account).UniqueReference) + d.Set("creation_date", (*the_bank_account).CreationDate) + d.Set("mandate_signature_date", (*the_bank_account).MandateSignatureDate) + d.Set("owner_name", (*the_bank_account).OwnerName) + d.Set("owner_address", (*the_bank_account).OwnerAddress) + d.Set("iban", (*the_bank_account).Iban) + d.Set("bic", (*the_bank_account).Bic) + + d.SetId(fmt.Sprintf("%d", (*the_bank_account).Id)) + return nil +} diff --git a/ovh/provider.go b/ovh/provider.go index 81f59fa04..fac5b878b 100644 --- a/ovh/provider.go +++ b/ovh/provider.go @@ -50,6 +50,7 @@ func Provider() terraform.ResourceProvider { "ovh_publiccloud_region": dataSourcePublicCloudRegion(), "ovh_publiccloud_regions": dataSourcePublicCloudRegions(), "ovh_credit_card": dataSourceCreditCard(), + "ovh_bank_account": dataSourceBankAccount(), }, ResourcesMap: map[string]*schema.Resource{ From 1229614be4ace1b35fa1c6052774187f37ae918f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Duchaussois?= Date: Tue, 22 May 2018 11:32:10 +0200 Subject: [PATCH 3/4] move datasource from legacy to new --- ovh/provider.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ovh/provider.go b/ovh/provider.go index fac5b878b..60a649cdd 100644 --- a/ovh/provider.go +++ b/ovh/provider.go @@ -46,11 +46,11 @@ func Provider() terraform.ResourceProvider { // New naming schema (issue #23) "ovh_cloud_region": dataSourcePublicCloudRegion(), "ovh_cloud_regions": dataSourcePublicCloudRegions(), + "ovh_credit_card": dataSourceCreditCard(), + "ovh_bank_account": dataSourceBankAccount(), // Legacy naming schema (new datasources should not be added here) "ovh_publiccloud_region": dataSourcePublicCloudRegion(), "ovh_publiccloud_regions": dataSourcePublicCloudRegions(), - "ovh_credit_card": dataSourceCreditCard(), - "ovh_bank_account": dataSourceBankAccount(), }, ResourcesMap: map[string]*schema.Resource{ From 73497ee822b2ae90001b80c30ec3481757c71d74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Duchaussois?= Date: Tue, 22 May 2018 17:36:21 +0200 Subject: [PATCH 4/4] remove potential sensitive information --- ovh/data_source_ovh_bankaccount.go | 45 ------------------------------ ovh/data_source_ovh_creditcard.go | 25 ----------------- 2 files changed, 70 deletions(-) diff --git a/ovh/data_source_ovh_bankaccount.go b/ovh/data_source_ovh_bankaccount.go index 2a0cde85f..843e4f253 100644 --- a/ovh/data_source_ovh_bankaccount.go +++ b/ovh/data_source_ovh_bankaccount.go @@ -55,42 +55,6 @@ func dataSourceBankAccount() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "validation_document_link": &schema.Schema{ - Type: schema.TypeString, - Computed: true, - }, - "unique_reference": &schema.Schema{ - Type: schema.TypeString, - Computed: true, - }, - "creation_date": &schema.Schema{ - Type: schema.TypeString, - Computed: true, - }, - "mandate_signature_date": &schema.Schema{ - Type: schema.TypeString, - Computed: true, - }, - "owner_name": &schema.Schema{ - Type: schema.TypeString, - Computed: true, - }, - "owner_address": &schema.Schema{ - Type: schema.TypeString, - Computed: true, - }, - "iban": &schema.Schema{ - Type: schema.TypeString, - Computed: true, - }, - "bic": &schema.Schema{ - Type: schema.TypeString, - Computed: true, - }, - "id": &schema.Schema{ - Type: schema.TypeInt, - Computed: true, - }, "default": &schema.Schema{ Type: schema.TypeBool, Computed: true, @@ -167,16 +131,7 @@ func dataSourceBankAccountRead(d *schema.ResourceData, meta interface{}) error { // Set data d.Set("description", (*the_bank_account).Description) d.Set("state", (*the_bank_account).State) - d.Set("id", (*the_bank_account).Id) d.Set("default", (*the_bank_account).Default) - d.Set("validation_document_link", (*the_bank_account).ValidationDocumentLink) - d.Set("unique_reference", (*the_bank_account).UniqueReference) - d.Set("creation_date", (*the_bank_account).CreationDate) - d.Set("mandate_signature_date", (*the_bank_account).MandateSignatureDate) - d.Set("owner_name", (*the_bank_account).OwnerName) - d.Set("owner_address", (*the_bank_account).OwnerAddress) - d.Set("iban", (*the_bank_account).Iban) - d.Set("bic", (*the_bank_account).Bic) d.SetId(fmt.Sprintf("%d", (*the_bank_account).Id)) return nil diff --git a/ovh/data_source_ovh_creditcard.go b/ovh/data_source_ovh_creditcard.go index b1958f0fd..d5d078df4 100644 --- a/ovh/data_source_ovh_creditcard.go +++ b/ovh/data_source_ovh_creditcard.go @@ -53,34 +53,14 @@ func dataSourceCreditCard() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "number": &schema.Schema{ - Type: schema.TypeString, - Computed: true, - }, - "expiration_date": &schema.Schema{ - Type: schema.TypeString, - Computed: true, - }, - "type": &schema.Schema{ - Type: schema.TypeString, - Computed: true, - }, "state": &schema.Schema{ Type: schema.TypeString, Computed: true, }, - "id": &schema.Schema{ - Type: schema.TypeInt, - Computed: true, - }, "default": &schema.Schema{ Type: schema.TypeBool, Computed: true, }, - "threeds_validated": &schema.Schema{ - Type: schema.TypeBool, - Computed: true, - }, }, } } @@ -164,13 +144,8 @@ func dataSourceCreditCardRead(d *schema.ResourceData, meta interface{}) error { } // Set data d.Set("description", (*the_credit_card).Description) - d.Set("number", (*the_credit_card).Number) - d.Set("expiration_date", (*the_credit_card).Expiration) - d.Set("type", (*the_credit_card).Type) d.Set("state", (*the_credit_card).State) - d.Set("id", (*the_credit_card).Id) d.Set("default", (*the_credit_card).Default) - d.Set("threeds_validated", (*the_credit_card).ThreeDSValidated) d.SetId(fmt.Sprintf("%d", (*the_credit_card).Id)) return nil }