-
Notifications
You must be signed in to change notification settings - Fork 132
/
data_me_paymentmean_creditcard.go
152 lines (146 loc) · 3.91 KB
/
data_me_paymentmean_creditcard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package ovh
import (
"fmt"
"regexp"
"sort"
"github.com/hashicorp/terraform-plugin-sdk/v2/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 dataSourceMePaymentmeanCreditcard() *schema.Resource {
return &schema.Resource{
Read: dataSourceMePaymentmeanCreditcardRead,
Schema: map[string]*schema.Schema{
"description_regexp": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Default: ".*",
},
"use_default": {
Type: schema.TypeBool,
ForceNew: true,
Optional: true,
Default: false,
},
"use_last_to_expire": {
Type: schema.TypeBool,
ForceNew: true,
Optional: true,
Default: false,
},
"states": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
},
// Computed
"description": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
"default": {
Type: schema.TypeBool,
Computed: true,
},
},
DeprecationMessage: "paymentmean API is deprecated. In order process, the account default payment method is used.",
}
}
func dataSourceMePaymentmeanCreditcardRead(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
}