|
| 1 | +package vra |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 5 | + "github.com/vmware/vra-sdk-go/pkg/client/image_profile" |
| 6 | + "github.com/vmware/vra-sdk-go/pkg/models" |
| 7 | + |
| 8 | + "fmt" |
| 9 | + "log" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +func dataSourceImageProfile() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Read: dataSourceImageProfileRead, |
| 16 | + |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "created_at": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Computed: true, |
| 21 | + Description: "Date when the entity was created. The date is in ISO 8601 and UTC.", |
| 22 | + }, |
| 23 | + "description": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Optional: true, |
| 26 | + Description: "A human-friendly description.", |
| 27 | + }, |
| 28 | + "external_region_id": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Computed: true, |
| 31 | + Description: "The id of the region for which this profile is defined as in the cloud provider.", |
| 32 | + }, |
| 33 | + "filter": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Optional: true, |
| 36 | + Description: "Filter query string that is supported by vRA multi-cloud IaaS API. Example: regionId eq '<regionId>' and cloudAccountId eq '<cloudAccountId>'. Only one of 'filter', 'id', 'name' or 'region_id' must be specified.", |
| 37 | + ConflictsWith: []string{"id", "name", "region_id"}, |
| 38 | + }, |
| 39 | + "id": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Optional: true, |
| 42 | + Computed: true, |
| 43 | + Description: "The id of the image profile instance. Only one of 'filter', 'id', 'name' or 'region_id' must be specified.", |
| 44 | + ConflictsWith: []string{"filter", "name", "region_id"}, |
| 45 | + }, |
| 46 | + "image_mapping": imageMappingSchema(), |
| 47 | + "name": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Optional: true, |
| 50 | + Computed: true, |
| 51 | + Description: "A human-friendly name used as an identifier in APIs that support this option. Only one of 'filter', 'id', 'name' or 'region_id' must be specified.", |
| 52 | + ConflictsWith: []string{"filter", "id", "region_id"}, |
| 53 | + }, |
| 54 | + "owner": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Computed: true, |
| 57 | + Description: "Email of the user that owns the entity.", |
| 58 | + }, |
| 59 | + "region_id": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Optional: true, |
| 62 | + Computed: true, |
| 63 | + Description: "The id of the region for which this profile is defined as in vRealize Automation(vRA). Only one of 'filter', 'id', 'name' or 'region_id' must be specified.", |
| 64 | + ConflictsWith: []string{"filter", "id", "name"}, |
| 65 | + }, |
| 66 | + "updated_at": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: "Date when the entity was last updated. The date is ISO 8601 and UTC.", |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func dataSourceImageProfileRead(d *schema.ResourceData, m interface{}) error { |
| 76 | + log.Printf("Reading the vra_image_profile data source with filter %s", d.Get("filter")) |
| 77 | + |
| 78 | + apiClient := m.(*Client).apiClient |
| 79 | + var imageProfile *models.ImageProfile |
| 80 | + var filter string |
| 81 | + |
| 82 | + id := d.Get("id").(string) |
| 83 | + name := d.Get("name").(string) |
| 84 | + configFilter := d.Get("filter").(string) |
| 85 | + regionID := d.Get("region_id").(string) |
| 86 | + |
| 87 | + if id == "" && name == "" && configFilter == "" && regionID == "" { |
| 88 | + return fmt.Errorf("one of (id, name, region_id, filter) is required") |
| 89 | + } |
| 90 | + |
| 91 | + setFields := func(account *models.ImageProfile) error { |
| 92 | + d.SetId(*account.ID) |
| 93 | + d.Set("created_at", imageProfile.CreatedAt) |
| 94 | + d.Set("description", imageProfile.Description) |
| 95 | + d.Set("external_region_id", imageProfile.ExternalRegionID) |
| 96 | + d.Set("name", imageProfile.Name) |
| 97 | + d.Set("owner", imageProfile.Owner) |
| 98 | + d.Set("updated_at", imageProfile.UpdatedAt) |
| 99 | + |
| 100 | + if regionLink, ok := imageProfile.Links["region"]; ok { |
| 101 | + if regionLink.Href != "" { |
| 102 | + d.Set("region_id", strings.TrimPrefix(regionLink.Href, "/iaas/api/regions/")) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if err := d.Set("image_mapping", flattenImageMappings(imageProfile.ImageMappings.Mapping)); err != nil { |
| 107 | + return fmt.Errorf("error setting image mappings - error: %#v", err) |
| 108 | + } |
| 109 | + return nil |
| 110 | + } |
| 111 | + |
| 112 | + if id != "" { |
| 113 | + getResp, err := apiClient.ImageProfile.GetImageProfile(image_profile.NewGetImageProfileParams().WithID(id)) |
| 114 | + |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + imageProfile = getResp.GetPayload() |
| 120 | + return setFields(imageProfile) |
| 121 | + |
| 122 | + } else if regionID != "" { |
| 123 | + filter = fmt.Sprintf("regionId eq '%v'", regionID) |
| 124 | + } else if name != "" { |
| 125 | + filter = fmt.Sprintf("name eq '%v'", name) |
| 126 | + } else if configFilter != "" { |
| 127 | + filter = configFilter |
| 128 | + } |
| 129 | + |
| 130 | + getResp, err := apiClient.ImageProfile.GetImageProfiles(image_profile.NewGetImageProfilesParams().WithDollarFilter(withString(filter))) |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + |
| 135 | + imageProfiles := *getResp.Payload |
| 136 | + if len(imageProfiles.Content) > 1 { |
| 137 | + return fmt.Errorf("vra_image_profile must filter to an image profile") |
| 138 | + } |
| 139 | + if len(imageProfiles.Content) == 0 { |
| 140 | + return fmt.Errorf("vra_image_profile filter did not match any image profile") |
| 141 | + } |
| 142 | + |
| 143 | + imageProfile = imageProfiles.Content[0] |
| 144 | + |
| 145 | + return setFields(imageProfile) |
| 146 | +} |
0 commit comments