-
Notifications
You must be signed in to change notification settings - Fork 66
Add GPU support: gpu_card / vgpu_profile data sources and GPU on service offerings #315
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
Open
poddm
wants to merge
14
commits into
apache:main
Choose a base branch
from
poddm:mp/service_gpus_clean
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1e0c02d
Adding gpu to service offerings
poddm c2f30d3
gpu
poddm 0ac5455
Address review comments
sureshanaparti 601973b
Fix GPU datasource filters and service offering state drift
sureshanaparti fb453af
Add CloudStack 4.22+ version check for GPU tests
sureshanaparti 262c60e
fix service offering commonread state
poddm 8bd74de
update gpu data source filtering
poddm 5d0ed07
Fix gpu tests name vs. id
poddm 9dfa020
fix computed gpu count
poddm ef12524
fix gpu count
poddm 5d8059b
fix offering nested blocks
poddm 37f25bd
Updating provider check for gpu profiles
poddm b04ef59
discover gpudevices
poddm bbdaf63
update data cloudstack_gpu_card test
poddm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,9 @@ website/node_modules | |
| *.iml | ||
| *.test | ||
| *.iml | ||
| dist/ | ||
| .gocache/ | ||
| .gomodcache/ | ||
|
|
||
| website/vendor | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| // | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| // | ||
|
|
||
| package cloudstack | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "strconv" | ||
|
|
||
| "github.com/apache/cloudstack-go/v2/cloudstack" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| ) | ||
|
|
||
| func dataSourceCloudstackGpuCard() *schema.Resource { | ||
| return &schema.Resource{ | ||
| Read: datasourceCloudStackGpuCardRead, | ||
| Schema: map[string]*schema.Schema{ | ||
| "filter": dataSourceFiltersSchema(), | ||
|
|
||
| //Computed values | ||
| "id": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| "name": { | ||
| Description: "the name of the GPU card", | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| "device_id": { | ||
| Description: "the device id of the GPU card", | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| "device_name": { | ||
| Description: "the device name of the GPU card", | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| "vendor_id": { | ||
| Description: "the vendor id of the GPU card", | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| "vendor_name": { | ||
| Description: "the vendor name of the GPU card", | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func datasourceCloudStackGpuCardRead(d *schema.ResourceData, meta interface{}) error { | ||
| cs := meta.(*cloudstack.CloudStackClient) | ||
| p := cs.GPU.NewListGpuCardsParams() | ||
|
|
||
| if err := applyGpuCardFilters(p, d.Get("filter").(*schema.Set)); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| csGpuCards, err := cs.GPU.ListGpuCards(p) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to list GPU cards: %s", err) | ||
| } | ||
|
|
||
| switch len(csGpuCards.GpuCards) { | ||
| case 0: | ||
| return fmt.Errorf("no GPU cards found") | ||
| case 1: | ||
| return gpuCardDescriptionAttributes(d, csGpuCards.GpuCards[0]) | ||
| default: | ||
| return fmt.Errorf("%d GPU cards matched the given filters; "+ | ||
| "refine the filters (e.g. add device_id or vendor_id) to match exactly one card", len(csGpuCards.GpuCards)) | ||
| } | ||
| } | ||
|
|
||
| func gpuCardDescriptionAttributes(d *schema.ResourceData, card *cloudstack.GpuCard) error { | ||
| d.SetId(card.Id) | ||
|
|
||
| fields := map[string]interface{}{ | ||
| "id": card.Id, | ||
| "name": card.Name, | ||
| "device_id": card.Deviceid, | ||
| "device_name": card.Devicename, | ||
| "vendor_id": card.Vendorid, | ||
| "vendor_name": card.Vendorname, | ||
| } | ||
|
|
||
| for k, v := range fields { | ||
| if err := d.Set(k, v); err != nil { | ||
| log.Printf("[WARN] Error setting %s: %s", k, err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func applyGpuCardFilters(p *cloudstack.ListGpuCardsParams, filters *schema.Set) error { | ||
| seen := make(map[string]bool) | ||
| for _, f := range filters.List() { | ||
| filter := f.(map[string]interface{}) | ||
| name := filter["name"].(string) | ||
| value := filter["value"].(string) | ||
|
|
||
| if seen[name] { | ||
| return fmt.Errorf("duplicate filter %q; each filter name may only be specified once", name) | ||
| } | ||
| seen[name] = true | ||
|
|
||
| switch name { | ||
| case "id": | ||
| p.SetId(value) | ||
| case "device_id": | ||
| p.SetDeviceid(value) | ||
| case "device_name": | ||
| p.SetDevicename(value) | ||
| case "vendor_id": | ||
| p.SetVendorid(value) | ||
| case "vendor_name": | ||
| p.SetVendorname(value) | ||
| case "keyword": | ||
| p.SetKeyword(value) | ||
| case "active_only": | ||
| b, err := strconv.ParseBool(value) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid boolean value %q for filter %q: %s", value, name, err) | ||
| } | ||
| p.SetActiveonly(b) | ||
| default: | ||
| return fmt.Errorf("unsupported filter %q; supported filters: id, device_id, device_name, vendor_id, vendor_name, keyword, active_only", name) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| // | ||
|
|
||
| package cloudstack | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| ) | ||
|
|
||
| func TestAccGpuCardDataSource_basic(t *testing.T) { | ||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { testAccPreCheckGPU(t) }, | ||
| Providers: testAccProviders, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: testGpuCardDataSourceConfig_basic, | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttrSet("data.cloudstack_gpu_card.test", "id"), | ||
| resource.TestCheckResourceAttr("data.cloudstack_gpu_card.test", "name", "Simulator Graphics Card Pro"), | ||
| ), | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| const testGpuCardDataSourceConfig_basic = ` | ||
| data "cloudstack_gpu_card" "test" { | ||
| filter { | ||
| name = "device_name" | ||
| value = "Graphics Card Pro" | ||
| } | ||
| } | ||
| ` | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.