Skip to content

Commit

Permalink
rp container services works, but gets back inconsistent status
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkunde committed Sep 12, 2023
1 parent 8aec786 commit 3e2d40f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 22 deletions.
22 changes: 11 additions & 11 deletions examples/provider-install-verification/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ resource "downtozero_container_service" "test1" {
}]
}

resource "downtozero_container_service" "test2" {
domains = [{
name = "08605de8-8baf-49bf-a820-4977ba694edc.containers.dtz.dev"
routing = [{
prefix = "/",
service_definition = {
container_image = "docker.io/library/nginx"
}
}]
}]
}
# resource "downtozero_container_service" "test2" {
# domains = [{
# name = "08605de8-8baf-49bf-a820-4977ba694edc.containers.dtz.dev"
# routing = [{
# prefix = "/",
# service_definition = {
# container_image = "docker.io/library/nginx"
# }
# }]
# }]
# }
7 changes: 5 additions & 2 deletions internal/provider/container_services_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"

"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
Expand Down Expand Up @@ -114,7 +115,7 @@ type containersServiceDefinitionModel struct {
func (d *containersDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state containerServiceModel

containersdomains, err := d.client.GetContainerServices()
containersdomains, err := d.client.GetContainerServices(ctx)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read DownToZero container services",
Expand Down Expand Up @@ -149,7 +150,7 @@ func (d *containersDataSource) Read(ctx context.Context, req datasource.ReadRequ
}

// Getcontainers - Returns list of containers (no auth required)
func (c *Client) GetContainerServices() ([]ContainersDomains, error) {
func (c *Client) GetContainerServices(ctx context.Context) ([]ContainersDomains, error) {

req, err := http.NewRequest("GET", fmt.Sprintf("%s/service", c.ApiUrl), nil)
if err != nil {
Expand All @@ -161,6 +162,8 @@ func (c *Client) GetContainerServices() ([]ContainersDomains, error) {
return nil, err
}

tflog.Debug(ctx, fmt.Sprintf("status: %d, body: %s", status, string(body[:])))

container_services := ContainerServices{}
err = json.Unmarshal(body, &container_services)
if err != nil {
Expand Down
70 changes: 61 additions & 9 deletions internal/provider/container_services_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ func (r *containersResource) Create(ctx context.Context, req resource.CreateRequ
// Create new containerservice

new_domains, err := r.client.CreateDomain(ctx, wrapper)
tflog.Error(ctx, err.Error())

if err != nil {
resp.Diagnostics.AddError(
Expand All @@ -136,6 +135,8 @@ func (r *containersResource) Create(ctx context.Context, req resource.CreateRequ
return
}

tflog.Info(ctx, fmt.Sprintf("new_domains -------\n%+v\n", new_domains))
tflog.Info(ctx, fmt.Sprintf("plan1 -------\n%+v\n", plan))
// Map response body to model
for _, domain := range new_domains {
var containersDomainsModel containersDomainsModel
Expand All @@ -155,6 +156,8 @@ func (r *containersResource) Create(ctx context.Context, req resource.CreateRequ
plan.Domains = append(plan.Domains, containersDomainsModel)
}

tflog.Info(ctx, fmt.Sprintf("plan2 -------\n%+v\n", plan))

// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
Expand All @@ -165,6 +168,51 @@ func (r *containersResource) Create(ctx context.Context, req resource.CreateRequ

// Read refreshes the Terraform state with the latest data.
func (r *containersResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Get current state
var state containerServiceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

// Get refreshed container service
new_domains, err := r.client.GetContainerServices(ctx)
if err != nil {
resp.Diagnostics.AddError(
"Error Reading container services",
"Could not read container service "+err.Error(),
)
return
}

// Overwrite items with refreshed state
state.Domains = []containersDomainsModel{}

for _, domain := range new_domains {
var containersDomainsModel containersDomainsModel
containersDomainsModel.Name = types.StringValue(domain.Name)

for _, routing := range domain.Routing {
var containersRoutingModel containersRoutingModel
containersRoutingModel.Prefix = types.StringValue(routing.Prefix)

var serviceDefinition containersServiceDefinitionModel
serviceDefinition.ContainerImage = types.StringValue(routing.ServiceDefinition.ContainerImage)

containersRoutingModel.ServiceDefinition = serviceDefinition
containersDomainsModel.Routing = append(containersDomainsModel.Routing, containersRoutingModel)
}

state.Domains = append(state.Domains, containersDomainsModel)
}

// Set refreshed state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

// Update updates the resource and sets the updated Terraform state on success.
Expand All @@ -191,7 +239,16 @@ func (c *Client) CreateDomain(ctx context.Context, containerServices ContainerSe

// var body []byte
status, body, err := c.doRequest(req)
tflog.Debug(ctx, fmt.Sprintf("status: %d, body: %s", status, string(body[:])))

if err != nil {
return nil, err
}

string_body := string(body[:])
string_body = strings.Trim(string_body, "\"")
string_body = strings.ReplaceAll(string_body, "\\\"", "\"")
tflog.Debug(ctx, fmt.Sprintf("status: %d, body: '%s'\n\n\n", status, string_body))

// for {
// var status int
// status, body, err = c.doRequest(req)
Expand All @@ -206,15 +263,10 @@ func (c *Client) CreateDomain(ctx context.Context, containerServices ContainerSe
// }
// }

if err != nil {
return nil, err
}

tflog.Debug(ctx, fmt.Sprintf("%+v", string(body[:])))

container_services := ContainerServices{}
err = json.Unmarshal(body, &container_services)
err = json.Unmarshal([]byte(string_body), &container_services)
if err != nil {
fmt.Println("aha")
return nil, err
}
return container_services.Domains, nil
Expand Down

0 comments on commit 3e2d40f

Please sign in to comment.