-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
data_source_batch_certificate.go
97 lines (81 loc) · 2.69 KB
/
data_source_batch_certificate.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
package azurerm
import (
"fmt"
"regexp"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func dataSourceArmBatchCertificate() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmBatchCertificateRead,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAzureRMBatchCertificateName,
},
"account_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAzureRMBatchAccountName,
},
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
"public_data": {
Type: schema.TypeString,
Computed: true,
},
"format": {
Type: schema.TypeString,
Computed: true,
},
"thumbprint": {
Type: schema.TypeString,
Computed: true,
},
"thumbprint_algorithm": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceArmBatchCertificateRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Batch.CertificateClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()
resourceGroupName := d.Get("resource_group_name").(string)
accountName := d.Get("account_name").(string)
name := d.Get("name").(string)
resp, err := client.Get(ctx, resourceGroupName, accountName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Batch certificate %q (Account %q / Resource Group %q) was not found", name, accountName, resourceGroupName)
}
return fmt.Errorf("Error making Read request on AzureRM Batch certificate %q: %+v", name, err)
}
d.SetId(*resp.ID)
d.Set("name", resp.Name)
d.Set("account_name", accountName)
d.Set("resource_group_name", resourceGroupName)
if props := resp.CertificateProperties; props != nil {
d.Set("format", props.Format)
d.Set("public_data", props.PublicData)
d.Set("thumbprint", props.Thumbprint)
d.Set("thumbprint_algorithm", props.ThumbprintAlgorithm)
}
return nil
}
func validateAzureRMBatchCertificateName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[\w]+-[\w]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"must be made up of algorithm and thumbprint separated by a dash in %q: %q", k, value))
}
return warnings, errors
}