Skip to content

Commit

Permalink
Introduces the use of IaaS-v1 API (#249)
Browse files Browse the repository at this point in the history
* Introduces the use of IaaS-v1 API

* Adjust go-mod

* Minor fix to move on to v1

---------

Co-authored-by: Robert Hoppe <robert.hoppe@mail.schwarz>
  • Loading branch information
roberth1988 and Robert Hoppe authored Jul 31, 2024
1 parent 7f6eb83 commit 12deca2
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 35 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/Masterminds/semver v1.5.0
github.com/SchwarzIT/community-stackit-go-client v1.30.2
github.com/SchwarzIT/community-stackit-go-client v1.30.5
github.com/go-test/deep v1.0.3
github.com/google/uuid v1.3.0
github.com/hashicorp/terraform-plugin-framework v1.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugX
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 h1:YoJbenK9C67SkzkDfmQuVln04ygHj3vjZfd9FL+GmQQ=
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo=
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
github.com/SchwarzIT/community-stackit-go-client v1.30.2 h1:g7A1dQXb1KSiM3LTJx560bo6g1+a13++cMLOXBa8twM=
github.com/SchwarzIT/community-stackit-go-client v1.30.2/go.mod h1:hlTfBNOKE1fokWE8g3KrI0AHo0SqzTKkS+LrIdhH8Qg=
github.com/SchwarzIT/community-stackit-go-client v1.30.5 h1:6IVN7wt30gw7CsPKcEsJ4sWsMD5w37++K1fyU55qspc=
github.com/SchwarzIT/community-stackit-go-client v1.30.5/go.mod h1:hlTfBNOKE1fokWE8g3KrI0AHo0SqzTKkS+LrIdhH8Qg=
github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk=
github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE=
Expand Down
12 changes: 7 additions & 5 deletions stackit/internal/data-sources/network/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (d DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *
projectID, _ := uuid.Parse(config.ProjectID.ValueString())
id, _ := uuid.Parse(config.ID.ValueString())

resNetwork, err := c.IAAS.V1GetNetwork(ctx, projectID, id)
resNetwork, err := c.IAAS.Network.V1GetNetwork(ctx, projectID, id)
if agg := common.Validate(&resp.Diagnostics, resNetwork, err, "JSON200"); agg != nil {
resp.Diagnostics.AddError("failed instance read", agg.Error())
return
Expand All @@ -42,8 +42,8 @@ func (d DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *
network := resNetwork.JSON200

prefixes := make([]attr.Value, 0)
if len(network.Prefixes) > 0 {
for _, pr := range network.Prefixes {
if network.Prefixes != nil && len(*network.Prefixes) > 0 {
for _, pr := range *network.Prefixes {
prefixes = append(prefixes, types.StringValue(pr))
}
}
Expand All @@ -63,8 +63,10 @@ func (d DataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *
config.NameServers = types.ListValueMust(types.StringType, nameservers)

// get the Prefix Length in a hacky way, otherwise fall back to default
if len(network.Prefixes) > 0 {
cidrSplit := strings.Split(network.Prefixes[0], "/")
if network.Prefixes != nil && len(*network.Prefixes) > 0 {
prefixData := *network.Prefixes

cidrSplit := strings.Split(prefixData[0], "/")
if len(cidrSplit) != 2 {
resp.Diagnostics.AddError("Processing CIDR Prefix Length",
"Processing CIDR Prefix Length")
Expand Down
2 changes: 1 addition & 1 deletion stackit/internal/data-sources/network/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package network
import (
"context"
"fmt"
iaas "github.com/SchwarzIT/community-stackit-go-client/pkg/services/iaas-api/v1alpha"
"github.com/SchwarzIT/community-stackit-go-client/pkg/services/iaas-api/v1"

"github.com/SchwarzIT/community-stackit-go-client/pkg/baseurl"
"github.com/SchwarzIT/community-stackit-go-client/pkg/services"
Expand Down
58 changes: 34 additions & 24 deletions stackit/internal/resources/network/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"

iaas "github.com/SchwarzIT/community-stackit-go-client/pkg/services/iaas-api/v1alpha"
iaas_network "github.com/SchwarzIT/community-stackit-go-client/pkg/services/iaas-api/v1/network"
clientValidate "github.com/SchwarzIT/community-stackit-go-client/pkg/validate"
)

Expand Down Expand Up @@ -45,7 +45,7 @@ func (r Resource) Create(ctx context.Context, req resource.CreateRequest, resp *
}

func (r Resource) createNetwork(ctx context.Context, resp *resource.CreateResponse, plan Network) Network {
var ns iaas.V1Nameserver
var ns iaas_network.V1Nameserver

for _, i := range plan.NameServers.Elements() {
if i.IsNull() || i.IsUnknown() {
Expand All @@ -63,15 +63,19 @@ func (r Resource) createNetwork(ctx context.Context, resp *resource.CreateRespon
pl := int(plan.PrefixLengthV4.ValueInt64())
name := plan.Name.ValueString()

body := iaas.V1CreateNetworkJSONRequestBody{
Name: name,
Nameservers: &ns,
PrefixLengthV4: &pl,
body := iaas_network.V1CreateNetworkJSONRequestBody{
Name: name,
AddressFamily: &iaas_network.V1CreateNetworkAddressFamily{
Ipv4: &iaas_network.V1CreateNetworkIPv4{
Nameservers: &ns,
PrefixLength: &pl,
},
},
}

projectID, _ := uuid.Parse(plan.ProjectID.String())

res, err := r.client.IAAS.V1CreateNetwork(ctx, projectID, body)
res, err := r.client.IAAS.Network.V1CreateNetwork(ctx, projectID, body)
if err != nil {
resp.Diagnostics.AddError(fmt.Sprintf("failed creating network %s", body.Name), err.Error())
return plan
Expand All @@ -82,23 +86,23 @@ func (r Resource) createNetwork(ctx context.Context, resp *resource.CreateRespon
return plan
}

process := res.WaitHandler(ctx, r.client.IAAS, projectID, name).SetTimeout(timeout)
process := res.WaitHandler(ctx, r.client.IAAS.Network, projectID, name).SetTimeout(timeout)
wr, err := process.WaitWithContext(ctx)
if err != nil {
resp.Diagnostics.AddError(fmt.Sprintf("failed validating network %s creation", body.Name), err.Error())
return plan
}

network, ok := wr.(iaas.V1Network)
network, ok := wr.(iaas_network.V1Network)
if !ok {
resp.Diagnostics.AddError("failed wait result conversion", "result is not of *iaas.V1Network")
resp.Diagnostics.AddError("failed wait result conversion", "result is not of *iaas_network.V1Network")
return plan
}

prefixes := make([]attr.Value, 0)

if len(network.Prefixes) > 0 {
for _, pr := range network.Prefixes {
if network.Prefixes != nil && len(*network.Prefixes) > 0 {
for _, pr := range *network.Prefixes {
prefixes = append(prefixes, types.StringValue(pr))
}
}
Expand All @@ -125,7 +129,7 @@ func (r Resource) Read(ctx context.Context, req resource.ReadRequest, resp *reso
projectID, _ := uuid.Parse(state.ProjectID.ValueString())
networkID, _ := uuid.Parse(state.ID.ValueString())

res, err := c.IAAS.V1GetNetwork(ctx, projectID, networkID)
res, err := c.IAAS.Network.V1GetNetwork(ctx, projectID, networkID)
if agg := common.Validate(&resp.Diagnostics, res, err, "JSON200"); agg != nil {
if validate.StatusEquals(res, http.StatusNotFound) {
resp.State.RemoveResource(ctx)
Expand All @@ -137,8 +141,8 @@ func (r Resource) Read(ctx context.Context, req resource.ReadRequest, resp *reso
n := res.JSON200

prefixes := make([]attr.Value, 0)
if len(n.Prefixes) > 0 {
for _, pr := range n.Prefixes {
if n.Prefixes != nil && len(*n.Prefixes) > 0 {
for _, pr := range *n.Prefixes {
prefixes = append(prefixes, types.StringValue(pr))
}
}
Expand All @@ -158,8 +162,10 @@ func (r Resource) Read(ctx context.Context, req resource.ReadRequest, resp *reso
state.NameServers = types.ListValueMust(types.StringType, nameservers)

// get the Prefix Length in a hacky way, otherwise fall back to default
if len(n.Prefixes) > 0 {
cidrSplit := strings.Split(n.Prefixes[0], "/")
if n.Prefixes != nil && len(*n.Prefixes) > 0 {
prefixData := *n.Prefixes

cidrSplit := strings.Split(prefixData[0], "/")
if len(cidrSplit) != 2 {
resp.Diagnostics.AddError("Processing CIDR Prefix Length",
"Processing CIDR Prefix Length")
Expand Down Expand Up @@ -222,7 +228,7 @@ func (r Resource) updateNetwork(ctx context.Context, plan, state Network, resp *
return
}

ns := make([]iaas.V1IP, 0)
ns := make([]iaas_network.V1IP, 0)
for _, s := range plan.NameServers.Elements() {
if s.IsNull() || s.IsUnknown() {
continue
Expand All @@ -233,15 +239,19 @@ func (r Resource) updateNetwork(ctx context.Context, plan, state Network, resp *

name := plan.Name.ValueString()

body := iaas.V1UpdateNetworkJSONBody{
Name: &name,
Nameservers: &ns,
body := iaas_network.V1UpdateNetworkJSONBody{
Name: &name,
AddressFamily: &iaas_network.V1UpdateNetworkAddressFamily{
Ipv4: &iaas_network.V1UpdateNetworkIPv4{
Nameservers: &ns,
},
},
}

projectID, _ := uuid.Parse(state.ProjectID.ValueString())
networkID, _ := uuid.Parse(state.ID.ValueString())

res, err := r.client.IAAS.V1UpdateNetwork(ctx, projectID, networkID, iaas.V1UpdateNetworkJSONRequestBody(body))
res, err := r.client.IAAS.Network.V1UpdateNetwork(ctx, projectID, networkID, iaas_network.V1UpdateNetworkJSONRequestBody(body))
if agg := common.Validate(&resp.Diagnostics, res, err, "JSON200"); agg != nil {
resp.Diagnostics.AddError("failed updating project", agg.Error())
return
Expand All @@ -260,13 +270,13 @@ func (r Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp *
networkID, _ := uuid.Parse(state.ID.ValueString())

c := r.client
res, err := c.IAAS.V1DeleteNetwork(ctx, projectID, networkID)
res, err := c.IAAS.Network.V1DeleteNetwork(ctx, projectID, networkID)
if agg := common.Validate(&resp.Diagnostics, res, err); agg != nil {
resp.Diagnostics.AddError("failed deleting network", agg.Error())
return
}

process := res.WaitHandler(ctx, c.IAAS, projectID, networkID)
process := res.WaitHandler(ctx, c.IAAS.Network, projectID, networkID)
if _, err = process.WaitWithContext(ctx); err != nil {
resp.Diagnostics.AddError("failed to verify network deletion", err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions stackit/internal/resources/network/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package network
import (
"context"
"fmt"
"github.com/SchwarzIT/community-stackit-go-client/pkg/services/iaas-api/v1alpha"
iaas_network "github.com/SchwarzIT/community-stackit-go-client/pkg/services/iaas-api/v1/network"
"github.com/SchwarzIT/terraform-provider-stackit/stackit/internal/common"
"github.com/SchwarzIT/terraform-provider-stackit/stackit/pkg/validate"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
Expand Down Expand Up @@ -46,7 +46,7 @@ func (r *Resource) Schema(ctx context.Context, req resource.SchemaRequest, resp
Description: "the name of the network",
Required: true,
Validators: []validator.String{
validate.StringWith(iaas.ValidateNetworkName, "validate network name"),
validate.StringWith(iaas_network.ValidateNetworkName, "validate network name"),
},
},
"nameservers": schema.ListAttribute{
Expand Down

0 comments on commit 12deca2

Please sign in to comment.