Skip to content

Commit

Permalink
Merge pull request #4 from crusoecloud/infiniband
Browse files Browse the repository at this point in the history
Infiniband
  • Loading branch information
agutierrez8 authored Aug 7, 2023
2 parents 9b9b813 + 4133e75 commit 65e5b87
Show file tree
Hide file tree
Showing 13 changed files with 350 additions and 14 deletions.
11 changes: 11 additions & 0 deletions .gitlab/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# By default, all Crusoe Cloud devs are codeowners of a file
* @crusoeenergy/island

.gitlab/CODEOWNERS @lanwall12 @nitper @ksosnowski3

# More restrictive ownership for adding dependencies and changing ci/builds
Makefile @lanwall12 @nitper @imackenzie @ksosnowski3 @agutierrez8
.golangci.yml @lanwall12 @nitper @imackenzie @ksosnowski3 @agutierrez8
.goreleaser.yml @lanwall12 @nitper @imackenzie @ksosnowski3 @agutierrez8
go.mod @lanwall12 @nitper @imackenzie @ksosnowski3 @agutierrez8
go.sum @lanwall12 @nitper @imackenzie @ksosnowski3 @agutierrez8
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ run:
# golangci.com configuration
# https://github.com/golangci/golangci/wiki/Configuration
service:
golangci-lint-version: 1.52.x # use a fixed version for consistent results
golangci-lint-version: 1.50.1 # use a fixed version for consistent results
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
GOLANGCI_VERSION = v1.50.1

default: install

generate:
Expand All @@ -9,5 +11,10 @@ install:
test:
go test -count=1 -parallel=4 ./...

lint:
precommit: test
go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_VERSION)
golangci-lint run --fix

lint:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_VERSION)
golangci-lint run
4 changes: 4 additions & 0 deletions crusoe/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/crusoecloud/terraform-provider-crusoe/internal"
"github.com/crusoecloud/terraform-provider-crusoe/internal/disk"
"github.com/crusoecloud/terraform-provider-crusoe/internal/firewall_rule"
"github.com/crusoecloud/terraform-provider-crusoe/internal/ib_network"
"github.com/crusoecloud/terraform-provider-crusoe/internal/ib_partition"
"github.com/crusoecloud/terraform-provider-crusoe/internal/vm"
)

Expand Down Expand Up @@ -48,6 +50,7 @@ func (p *crusoeProvider) DataSources(_ context.Context) []func() datasource.Data
return []func() datasource.DataSource{
vm.NewVMDataSource,
disk.NewDisksDataSource,
ib_network.NewIBNetworkDataSource,
}
}

Expand All @@ -57,6 +60,7 @@ func (p *crusoeProvider) Resources(_ context.Context) []func() resource.Resource
vm.NewVMResource,
disk.NewDiskResource,
firewall_rule.NewFirewallRuleResource,
ib_partition.NewIBPartitionResource,
}
}

Expand Down
48 changes: 48 additions & 0 deletions examples/infiniband/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
terraform {
required_providers {
crusoe = {
source = "registry.terraform.io/crusoecloud/crusoe"
}
}
}

locals {
my_ssh_key = file("~/.ssh/id_ed25519.pub")
}

# list IB networks
data "crusoe_ib_networks" "ib_networks" {}
output "crusoe_ib" {
value = data.crusoe_ib_networks.ib_networks
}

# create an IB partition to deploy VMs in
resource "crusoe_ib_partition" "my_partition" {
name = "my-ib-partition"

# available IB network IDs can be listed by using the output
# above. alternatively, they can be obtain with the CLI by
# crusoe networking ib-network list
ib_network_id = "<ib_network_id>"
}

# create two VMs, both in the same Infiniband partition
resource "crusoe_compute_instance" "my_vm1" {
count = 2

name = "ib-vm-${count.index}"
type = "h100-80gb-hgx.8x" # IB enabled VM type
ib_partition_id = crusoe_ib_partition.my_partition.id
ssh_key = local.my_ssh_key

disks = [
// disk attached at startup
crusoe_storage_disk.data_disk
]
}

# attached storage disk
resource "crusoe_storage_disk" "data_disk" {
name = "data-disk"
size = "1TiB"
}
4 changes: 2 additions & 2 deletions examples/vms/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ resource "crusoe_compute_instance" "my_vm" {
startup_script = file("startup.sh")

disks = [
// uncomment to attach at startup
// crusoe_storage_disk.data_disk
// attached at startup
crusoe_storage_disk.data_disk
]
}

Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ go 1.18
require (
github.com/BurntSushi/toml v0.3.1
github.com/antihax/optional v1.0.0
github.com/crusoecloud/client-go v0.1.19
github.com/hashicorp/terraform-plugin-framework v1.2.0
)

require github.com/crusoecloud/client-go v0.1.19

require (
github.com/fatih/color v1.13.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
Expand Down
4 changes: 1 addition & 3 deletions internal/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ func canonicalizeQuery(query string) (canonicalQuery string, err error) {
continue
}
value := ""
if i := strings.Index(key, "="); i >= 0 {
key, value = key[:i], key[i+1:]
}
key, value, _ = strings.Cut(key, "=")
key, err1 := url.QueryUnescape(key)
if err1 != nil {
if err == nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/firewall_rule/firewall_rule_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ func (r *firewallRuleResource) Read(ctx context.Context, req resource.ReadReques
//nolint:gocritic // Implements Terraform defined interface
func (r *firewallRuleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// This should be unreachable, since all properties are marked as needing replacement on update.
resp.Diagnostics.AddWarning("Updates not supported",
"Updating firewall rules is not currently supported. If you're seeing this message, please reach out to"+
" [email protected] and let us know. In the meantime, you should be able to update your rule by"+
" deleting it and then creating a new one.")
resp.Diagnostics.AddWarning("In-place updates not supported",
"Updating firewall rules in-place is not currently supported. If you're seeing this message, please reach"+
" out to [email protected] and let us know. In the meantime, you should be able to update your"+
" rule by deleting it and then creating a new one.")
}

//nolint:gocritic // Implements Terraform defined interface
Expand Down
95 changes: 95 additions & 0 deletions internal/ib_network/ib_network_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//nolint:gocritic // Implements Terraform defined interface
package ib_network

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"

swagger "github.com/crusoecloud/client-go/swagger/v1alpha4"
"github.com/crusoecloud/terraform-provider-crusoe/internal"
)

type ibNetworksDataSource struct {
client *swagger.APIClient
}

type ibNetworksDataSourceModel struct {
IBNetworks []ibNetworkModel `tfsdk:"ib_networks"`
}

type ibNetworkModel struct {
ID string `tfsdk:"id"`
Name string `tfsdk:"name"`
Location string `tfsdk:"location"`
}

func NewIBNetworkDataSource() datasource.DataSource {
return &ibNetworksDataSource{}
}

// Configure adds the provider configured client to the data source.
func (ds *ibNetworksDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*swagger.APIClient)
if !ok {
resp.Diagnostics.AddError("Failed to initialize provider", internal.ErrorMsgProviderInitFailed)

return
}

ds.client = client
}

func (ds *ibNetworksDataSource) Metadata(ctx context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) {
response.TypeName = request.ProviderTypeName + "_ib_networks"
}

func (ds *ibNetworksDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) {
response.Schema = schema.Schema{Attributes: map[string]schema.Attribute{
"ib_networks": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"name": schema.StringAttribute{
Computed: true,
},
"location": schema.StringAttribute{
Computed: true,
},
},
},
},
}}
}

func (ds *ibNetworksDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
dataResp, httpResp, err := ds.client.IBNetworksApi.GetIBNetworks(ctx)
if err != nil {
resp.Diagnostics.AddError("Failed to Fetch IB Networks",
fmt.Sprintf("Could not fetch Infiniband network data at this time: %v", err))

return
}
defer httpResp.Body.Close()

var state ibNetworksDataSourceModel
for i := range dataResp.IbNetworks {
state.IBNetworks = append(state.IBNetworks, ibNetworkModel{
ID: dataResp.IbNetworks[i].Id,
Name: dataResp.IbNetworks[i].Name,
Location: dataResp.IbNetworks[i].Location,
})
}

diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
}
Loading

0 comments on commit 65e5b87

Please sign in to comment.