Skip to content

Commit f596d03

Browse files
committed
add ovh_bank_account data source
1 parent 3928529 commit f596d03

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed

ovh/data_source_ovh_bankaccount.go

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"sort"
7+
8+
"github.com/hashicorp/terraform/helper/schema"
9+
)
10+
11+
type BankAccount struct {
12+
Description string `json:"description"`
13+
Default bool `json:"defaultPaymentMean"`
14+
State string `json:"state"`
15+
Id int `json:"id"`
16+
ValidationDocumentLink string `json:"validationDocumentLink"`
17+
UniqueReference string `json:"uniqueReference"`
18+
CreationDate string `json:"creationDate"`
19+
MandateSignatureDate string `json:"mandateSignatureDate"`
20+
OwnerName string `json:"ownerName"`
21+
OwnerAddress string `json:"ownerAddress"`
22+
Iban string `json:"iban"`
23+
Bic string `json:"bic"`
24+
}
25+
26+
func dataSourceBankAccount() *schema.Resource {
27+
return &schema.Resource{
28+
Read: dataSourceBankAccountRead,
29+
Schema: map[string]*schema.Schema{
30+
"description_regexp": &schema.Schema{
31+
Type: schema.TypeString,
32+
ForceNew: true,
33+
Optional: true,
34+
Default: ".*",
35+
},
36+
"use_default": &schema.Schema{
37+
Type: schema.TypeBool,
38+
ForceNew: true,
39+
Optional: true,
40+
Default: false,
41+
},
42+
"use_oldest": &schema.Schema{
43+
Type: schema.TypeBool,
44+
ForceNew: true,
45+
Optional: true,
46+
Default: false,
47+
},
48+
"state": &schema.Schema{
49+
Type: schema.TypeString,
50+
Optional: true,
51+
Computed: true,
52+
},
53+
// Computed
54+
"description": &schema.Schema{
55+
Type: schema.TypeString,
56+
Computed: true,
57+
},
58+
"validation_document_link": &schema.Schema{
59+
Type: schema.TypeString,
60+
Computed: true,
61+
},
62+
"unique_reference": &schema.Schema{
63+
Type: schema.TypeString,
64+
Computed: true,
65+
},
66+
"creation_date": &schema.Schema{
67+
Type: schema.TypeString,
68+
Computed: true,
69+
},
70+
"mandate_signature_date": &schema.Schema{
71+
Type: schema.TypeString,
72+
Computed: true,
73+
},
74+
"owner_name": &schema.Schema{
75+
Type: schema.TypeString,
76+
Computed: true,
77+
},
78+
"owner_address": &schema.Schema{
79+
Type: schema.TypeString,
80+
Computed: true,
81+
},
82+
"iban": &schema.Schema{
83+
Type: schema.TypeString,
84+
Computed: true,
85+
},
86+
"bic": &schema.Schema{
87+
Type: schema.TypeString,
88+
Computed: true,
89+
},
90+
"id": &schema.Schema{
91+
Type: schema.TypeInt,
92+
Computed: true,
93+
},
94+
"default": &schema.Schema{
95+
Type: schema.TypeBool,
96+
Computed: true,
97+
},
98+
},
99+
}
100+
}
101+
102+
func dataSourceBankAccountRead(d *schema.ResourceData, meta interface{}) error {
103+
config := meta.(*Config)
104+
state, state_ok := d.GetOk("state")
105+
description_regexp := regexp.MustCompile(d.Get("description_regexp").(string))
106+
use_oldest := d.Get("use_oldest").(bool)
107+
use_default := d.Get("use_default").(bool)
108+
var the_bank_account *BankAccount
109+
var bank_account_ids []int
110+
endpoint := "/me/paymentMean/bankAccount"
111+
if state_ok {
112+
endpoint = fmt.Sprintf("%s?state=%s", endpoint, state)
113+
}
114+
err := config.OVHClient.Get(
115+
endpoint,
116+
&bank_account_ids,
117+
)
118+
119+
if err != nil {
120+
return fmt.Errorf("Error getting Bank Account list:\n\t %q", err)
121+
}
122+
filtered_bank_accounts := []*BankAccount{}
123+
for _, account_id := range bank_account_ids {
124+
bank_account := BankAccount{}
125+
err = config.OVHClient.Get(
126+
fmt.Sprintf("/me/paymentMean/bankAccount/%d", account_id),
127+
&bank_account,
128+
)
129+
if err != nil {
130+
return fmt.Errorf("Error getting Bank Account %d:\n\t %q", account_id, err)
131+
}
132+
if use_default && bank_account.Default == false {
133+
continue
134+
}
135+
if !description_regexp.MatchString(bank_account.Description) {
136+
continue
137+
}
138+
filtered_bank_accounts = append(filtered_bank_accounts, &bank_account)
139+
}
140+
if len(filtered_bank_accounts) < 1 {
141+
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
142+
}
143+
if len(filtered_bank_accounts) > 1 {
144+
if use_oldest {
145+
sort.Slice(filtered_bank_accounts, func(i, j int) bool {
146+
return (*filtered_bank_accounts[i]).CreationDate < (*filtered_bank_accounts[j]).CreationDate
147+
})
148+
the_bank_account = filtered_bank_accounts[0]
149+
}
150+
if use_default {
151+
match := false
152+
for _, bank_account := range filtered_bank_accounts {
153+
if (*bank_account).Default {
154+
match = true
155+
the_bank_account = bank_account
156+
break
157+
}
158+
}
159+
if match == false {
160+
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
161+
}
162+
}
163+
}
164+
if len(filtered_bank_accounts) == 1 {
165+
the_bank_account = filtered_bank_accounts[0]
166+
}
167+
// Set data
168+
d.Set("description", (*the_bank_account).Description)
169+
d.Set("state", (*the_bank_account).State)
170+
d.Set("id", (*the_bank_account).Id)
171+
d.Set("default", (*the_bank_account).Default)
172+
d.Set("validation_document_link", (*the_bank_account).ValidationDocumentLink)
173+
d.Set("unique_reference", (*the_bank_account).UniqueReference)
174+
d.Set("creation_date", (*the_bank_account).CreationDate)
175+
d.Set("mandate_signature_date", (*the_bank_account).MandateSignatureDate)
176+
d.Set("owner_name", (*the_bank_account).OwnerName)
177+
d.Set("owner_address", (*the_bank_account).OwnerAddress)
178+
d.Set("iban", (*the_bank_account).Iban)
179+
d.Set("bic", (*the_bank_account).Bic)
180+
181+
d.SetId(fmt.Sprintf("%d", (*the_bank_account).Id))
182+
return nil
183+
}

ovh/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func Provider() terraform.ResourceProvider {
5050
"ovh_publiccloud_region": dataSourcePublicCloudRegion(),
5151
"ovh_publiccloud_regions": dataSourcePublicCloudRegions(),
5252
"ovh_credit_card": dataSourceCreditCard(),
53+
"ovh_bank_account": dataSourceBankAccount(),
5354
},
5455

5556
ResourcesMap: map[string]*schema.Resource{

0 commit comments

Comments
 (0)