-
Notifications
You must be signed in to change notification settings - Fork 144
Payment data sources #34
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
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, | ||
}, | ||
"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("default", (*the_bank_account).Default) | ||
|
||
d.SetId(fmt.Sprintf("%d", (*the_bank_account).Id)) | ||
return nil | ||
} |
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,151 @@ | ||
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, | ||
}, | ||
"state": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"default": &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("state", (*the_credit_card).State) | ||
d.Set("default", (*the_credit_card).Default) | ||
d.SetId(fmt.Sprintf("%d", (*the_credit_card).Id)) | ||
return nil | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we map the ovh api by prefixing datasources by "ovh_me_" ?
pls ? and rename the go functions accordingly ?
thanks a lot