Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added filters in vpc service data sources #4119

Merged
merged 1 commit into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 75 additions & 2 deletions ibm/service/vpc/data_source_ibm_is_bare_metal_servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,46 @@ func DataSourceIBMIsBareMetalServers() *schema.Resource {
ReadContext: dataSourceIBMISBareMetalServersRead,

Schema: map[string]*schema.Schema{

"resource_group": {
Type: schema.TypeString,
Optional: true,
Description: "The unique identifier of the resource group this bare metal server belongs to",
},
"vpc": {
Type: schema.TypeString,
Optional: true,
Description: "The vpc ID this bare metal server is in",
},
"vpc_name": {
Type: schema.TypeString,
Optional: true,
Description: "The vpc name this bare metal server is in",
},
"vpc_crn": {
Type: schema.TypeString,
Optional: true,
Description: "The vpc CRN this bare metal server is in",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the bare metal server",
},
"network_interfaces_subnet": {
Type: schema.TypeString,
Optional: true,
Description: "The ID of the subnet of the bare metal server network interfaces",
},
"network_interfaces_subnet_crn": {
Type: schema.TypeString,
Optional: true,
Description: "The crn of the subnet of the bare metal server network interfaces",
},
"network_interfaces_subnet_name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the subnet of the bare metal server network interfaces",
},
isBareMetalServers: {
Type: schema.TypeList,
Description: "List of Bare Metal Servers",
Expand Down Expand Up @@ -386,8 +425,42 @@ func dataSourceIBMISBareMetalServersRead(context context.Context, d *schema.Reso
}
start := ""
allrecs := []vpcv1.BareMetalServer{}

listBareMetalServersOptions := &vpcv1.ListBareMetalServersOptions{}
if resgroupintf, ok := d.GetOk("resource_group"); ok {
resGroup := resgroupintf.(string)
listBareMetalServersOptions.ResourceGroupID = &resGroup
}
if nameintf, ok := d.GetOk("name"); ok {
name := nameintf.(string)
listBareMetalServersOptions.Name = &name
}
if vpcIntf, ok := d.GetOk("vpc"); ok {
vpcid := vpcIntf.(string)
listBareMetalServersOptions.VPCID = &vpcid
}
if vpcNameIntf, ok := d.GetOk("vpc_name"); ok {
vpcName := vpcNameIntf.(string)
listBareMetalServersOptions.VPCName = &vpcName
}
if vpcCrnIntf, ok := d.GetOk("vpc_crn"); ok {
vpcCrn := vpcCrnIntf.(string)
listBareMetalServersOptions.VPCCRN = &vpcCrn
}
if subnetIntf, ok := d.GetOk("network_interfaces_subnet"); ok {
subnetId := subnetIntf.(string)
listBareMetalServersOptions.NetworkInterfacesSubnetID = &subnetId
}
if subnetNameIntf, ok := d.GetOk("network_interfaces_subnet_name"); ok {
subnetName := subnetNameIntf.(string)
listBareMetalServersOptions.NetworkInterfacesSubnetName = &subnetName
}
if subnetCrnIntf, ok := d.GetOk("network_interfaces_subnet_crn"); ok {
subnetCrn := subnetCrnIntf.(string)
listBareMetalServersOptions.NetworkInterfacesSubnetCRN = &subnetCrn
}
for {
listBareMetalServersOptions := &vpcv1.ListBareMetalServersOptions{}

if start != "" {
listBareMetalServersOptions.Start = &start
}
Expand Down
222 changes: 111 additions & 111 deletions ibm/service/vpc/data_source_ibm_is_dedicated_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,126 +350,126 @@ func dataSourceIbmIsDedicatedHostRead(context context.Context, d *schema.Resourc
resgrpidstr := resgrpid.(string)
listDedicatedHostsOptions.ResourceGroupID = &resgrpidstr
}
name := d.Get("name").(string)
listDedicatedHostsOptions.Name = &name
dedicatedHostCollection, response, err := vpcClient.ListDedicatedHostsWithContext(context, listDedicatedHostsOptions)
if err != nil {
log.Printf("[DEBUG] ListDedicatedHostsWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
name := d.Get("name").(string)

if len(dedicatedHostCollection.DedicatedHosts) != 0 {
dedicatedHost := vpcv1.DedicatedHost{}
for _, data := range dedicatedHostCollection.DedicatedHosts {
if *data.Name == name {
dedicatedHost = data
d.SetId(*dedicatedHost.ID)

if err = d.Set("available_memory", dedicatedHost.AvailableMemory); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting available_memory: %s", err))
}

if dedicatedHost.AvailableVcpu != nil {
err = d.Set("available_vcpu", dataSourceDedicatedHostFlattenAvailableVcpu(*dedicatedHost.AvailableVcpu))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting available_vcpu %s", err))
}
}
if err = d.Set("created_at", dedicatedHost.CreatedAt.String()); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting created_at: %s", err))
}
if err = d.Set("crn", dedicatedHost.CRN); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting crn: %s", err))
}
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *dedicatedHost.CRN, "", isDedicatedHostAccessTagType)
if err != nil {
log.Printf(
"Error on get of resource dedicated host (%s) access tags: %s", d.Id(), err)
}
d.Set(isDedicatedHostAccessTags, accesstags)
if dedicatedHost.Disks != nil {
err = d.Set("disks", dataSourceDedicatedHostFlattenDisks(dedicatedHost.Disks))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting disks %s", err))
}
}
if dedicatedHost.Group != nil {
err = d.Set("host_group", *dedicatedHost.Group.ID)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting group %s", err))
}
}
if err = d.Set("href", dedicatedHost.Href); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting href: %s", err))
}
if err = d.Set("instance_placement_enabled", dedicatedHost.InstancePlacementEnabled); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting instance_placement_enabled: %s", err))
}

if dedicatedHost.Instances != nil {
err = d.Set("instances", dataSourceDedicatedHostFlattenInstances(dedicatedHost.Instances))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting instances %s", err))
}
}
if err = d.Set("lifecycle_state", dedicatedHost.LifecycleState); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting lifecycle_state: %s", err))
}
if err = d.Set("memory", dedicatedHost.Memory); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting memory: %s", err))
}
if err = d.Set("name", dedicatedHost.Name); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting name: %s", err))
}

if dedicatedHost.Profile != nil {
err = d.Set("profile", dataSourceDedicatedHostFlattenProfile(*dedicatedHost.Profile))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting profile %s", err))
}
}
if err = d.Set("provisionable", dedicatedHost.Provisionable); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting provisionable: %s", err))
}

if dedicatedHost.ResourceGroup != nil {
err = d.Set("resource_group", *dedicatedHost.ResourceGroup.ID)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting resource_group %s", err))
}
}
if err = d.Set("resource_type", dedicatedHost.ResourceType); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting resource_type: %s", err))
}
if err = d.Set("socket_count", dedicatedHost.SocketCount); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting socket_count: %s", err))
}
if err = d.Set("state", dedicatedHost.State); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting state: %s", err))
}

if dedicatedHost.SupportedInstanceProfiles != nil {
err = d.Set("supported_instance_profiles", dataSourceDedicatedHostFlattenSupportedInstanceProfiles(dedicatedHost.SupportedInstanceProfiles))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting supported_instance_profiles %s", err))
}
}

if dedicatedHost.Vcpu != nil {
err = d.Set("vcpu", dataSourceDedicatedHostFlattenVcpu(*dedicatedHost.Vcpu))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting vcpu %s", err))
}
}

if dedicatedHost.Zone != nil {
err = d.Set("zone", *dedicatedHost.Zone.Name)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting zone %s", err))
}
}

return nil

dedicatedHost = dedicatedHostCollection.DedicatedHosts[0]
d.SetId(*dedicatedHost.ID)

if err = d.Set("available_memory", dedicatedHost.AvailableMemory); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting available_memory: %s", err))
}

if dedicatedHost.AvailableVcpu != nil {
err = d.Set("available_vcpu", dataSourceDedicatedHostFlattenAvailableVcpu(*dedicatedHost.AvailableVcpu))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting available_vcpu %s", err))
}
}
if err = d.Set("created_at", dedicatedHost.CreatedAt.String()); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting created_at: %s", err))
}
if err = d.Set("crn", dedicatedHost.CRN); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting crn: %s", err))
}
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *dedicatedHost.CRN, "", isDedicatedHostAccessTagType)
if err != nil {
log.Printf(
"Error on get of resource dedicated host (%s) access tags: %s", d.Id(), err)
}
d.Set(isDedicatedHostAccessTags, accesstags)
if dedicatedHost.Disks != nil {
err = d.Set("disks", dataSourceDedicatedHostFlattenDisks(dedicatedHost.Disks))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting disks %s", err))
}
}
if dedicatedHost.Group != nil {
err = d.Set("host_group", *dedicatedHost.Group.ID)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting group %s", err))
}
}
if err = d.Set("href", dedicatedHost.Href); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting href: %s", err))
}
if err = d.Set("instance_placement_enabled", dedicatedHost.InstancePlacementEnabled); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting instance_placement_enabled: %s", err))
}

if dedicatedHost.Instances != nil {
err = d.Set("instances", dataSourceDedicatedHostFlattenInstances(dedicatedHost.Instances))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting instances %s", err))
}
}
if err = d.Set("lifecycle_state", dedicatedHost.LifecycleState); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting lifecycle_state: %s", err))
}
if err = d.Set("memory", dedicatedHost.Memory); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting memory: %s", err))
}
if err = d.Set("name", dedicatedHost.Name); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting name: %s", err))
}

if dedicatedHost.Profile != nil {
err = d.Set("profile", dataSourceDedicatedHostFlattenProfile(*dedicatedHost.Profile))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting profile %s", err))
}
}
if err = d.Set("provisionable", dedicatedHost.Provisionable); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting provisionable: %s", err))
}

if dedicatedHost.ResourceGroup != nil {
err = d.Set("resource_group", *dedicatedHost.ResourceGroup.ID)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting resource_group %s", err))
}
}
if err = d.Set("resource_type", dedicatedHost.ResourceType); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting resource_type: %s", err))
}
if err = d.Set("socket_count", dedicatedHost.SocketCount); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting socket_count: %s", err))
}
if err = d.Set("state", dedicatedHost.State); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting state: %s", err))
}

if dedicatedHost.SupportedInstanceProfiles != nil {
err = d.Set("supported_instance_profiles", dataSourceDedicatedHostFlattenSupportedInstanceProfiles(dedicatedHost.SupportedInstanceProfiles))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting supported_instance_profiles %s", err))
}
}

if dedicatedHost.Vcpu != nil {
err = d.Set("vcpu", dataSourceDedicatedHostFlattenVcpu(*dedicatedHost.Vcpu))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting vcpu %s", err))
}
}

if dedicatedHost.Zone != nil {
err = d.Set("zone", *dedicatedHost.Zone.Name)
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting zone %s", err))
}
}

return nil

}
return diag.FromErr(fmt.Errorf("[ERROR] No Dedicated Host found with name %s", name))
}
Expand Down
Loading