-
Notifications
You must be signed in to change notification settings - Fork 10
/
diskencryptionconfigurations_data_source.go
93 lines (81 loc) · 3.23 KB
/
diskencryptionconfigurations_data_source.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
// diskencryptionconfigurations_resource.go
package diskencryptionconfigurations
import (
"context"
"fmt"
"strconv"
"time"
"github.com/deploymenttheory/go-api-sdk-jamfpro/sdk/jamfpro"
"github.com/deploymenttheory/terraform-provider-jamfpro/internal/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// DataSourceJamfProDiskEncryptionConfigurations defines the schema and CRUD operations for managing Jamf Pro Disk Encryption Configurations in Terraform.
func DataSourceJamfProDiskEncryptionConfigurations() *schema.Resource {
return &schema.Resource{
ReadContext: DataSourceJamfProDiskEncryptionConfigurationsRead,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(30 * time.Second),
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
Description: "The unique identifier of the disk encryption configuration.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the disk encryption configuration.",
},
},
}
}
// DataSourceJamfProDiskEncryptionConfigurationsRead fetches the details of a specific Jamf Pro disk encryption configuration
// from Jamf Pro and returns the details of the disk encryption configuration in the Terraform state.
func DataSourceJamfProDiskEncryptionConfigurationsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// Initialize API client
apiclient, ok := meta.(*client.APIClient)
if !ok {
return diag.Errorf("error asserting meta as *client.APIClient")
}
conn := apiclient.Conn
// Initialize variables
var diags diag.Diagnostics
resourceID := d.Get("id").(string)
// Convert resourceID from string to int
resourceIDInt, err := strconv.Atoi(resourceID)
if err != nil {
return diag.FromErr(fmt.Errorf("error converting resource ID '%s' to int: %v", resourceID, err))
}
var resource *jamfpro.ResourceDiskEncryptionConfiguration
// Read operation with retry
err = retry.RetryContext(ctx, d.Timeout(schema.TimeoutRead), func() *retry.RetryError {
var apiErr error
resource, apiErr = conn.GetDiskEncryptionConfigurationByID(resourceIDInt)
if apiErr != nil {
// Convert any API error into a retryable error to continue retrying
return retry.RetryableError(apiErr)
}
// Successfully read the resource, exit the retry loop
return nil
})
if err != nil {
// Handle the final error after all retries have been exhausted
return diag.FromErr(fmt.Errorf("failed to read Jamf Pro Disk Encryption Configuration with ID '%s' after retries: %v", resourceID, err))
}
// Check if resource data exists and set the Terraform state
if resource != nil {
d.SetId(resourceID) // Confirm the ID in the Terraform state
if err := d.Set("name", resource.Name); err != nil {
diags = append(diags, diag.FromErr(fmt.Errorf("error setting 'name' for Jamf Pro Disk Encryption Configuration with ID '%s': %v", resourceID, err))...)
}
} else {
d.SetId("") // Data not found, unset the ID in the Terraform state
}
return diags
}