Skip to content

Commit

Permalink
SYSENG-1658: add e5e resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Schäfer authored and anx-mschaefer committed Mar 19, 2024
1 parent 1a6bd14 commit 1eb8516
Show file tree
Hide file tree
Showing 12 changed files with 1,121 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ If the change isn't user-facing but still relevant enough for a changelog entry,
### Changed
* (internal) resource/anxcloud_virtual_server: optimize creation of vms with multiple disks (#147, @anx-mschaefer)

### Added
* resources to manage the e5e service: (#142, @anx-mschaefer)
* `anxcloud_e5e_application`
* `anxcloud_e5e_function`

## [0.5.5] - 2024-01-12

### Fixed
Expand Down
31 changes: 31 additions & 0 deletions anxcloud/internal/apis/e5e/v1/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package v1

import (
"context"
"errors"
"fmt"
"net/url"

"go.anx.io/go-anxcloud/pkg/api/types"
)

type E5EFunctionDeployment struct {
FunctionIdentifier string `json:"-"`
}

func (d *E5EFunctionDeployment) GetIdentifier(ctx context.Context) (string, error) {
return "", nil
}

func (d *E5EFunctionDeployment) EndpointURL(ctx context.Context) (*url.URL, error) {
op, err := types.OperationFromContext(ctx)
if err != nil {
return nil, err
}

if op != types.OperationCreate {
return nil, errors.New("helper resource 'E5EFunctionDeployment' only supports create operations")
}

return url.Parse(fmt.Sprintf("/api/e5e/v1/function.json/%s/deploy", d.FunctionIdentifier))
}
2 changes: 2 additions & 0 deletions anxcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func Provider(version string) *schema.Provider {
"anxcloud_kubernetes_cluster": resourceKubernetesCluster(),
"anxcloud_kubernetes_node_pool": resourceKubernetesNodePool(),
"anxcloud_kubernetes_kubeconfig": resourceKubernetesKubeconfig(),
"anxcloud_e5e_application": resourceE5EApplication(),
"anxcloud_e5e_function": resourceE5EFunction(),
},
DataSourcesMap: map[string]*schema.Resource{
"anxcloud_disk_types": dataSourceDiskTypes(),
Expand Down
98 changes: 98 additions & 0 deletions anxcloud/resource_e5e_application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package anxcloud

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"go.anx.io/go-anxcloud/pkg/api"

e5ev1 "go.anx.io/go-anxcloud/pkg/apis/e5e/v1"
)

func resourceE5EApplication() *schema.Resource {
return &schema.Resource{
Description: "Applications are an easy way to bring more structure to your configured functions by grouping them." +
" You can imagine an application as a folder to put in your functions.",
CreateContext: resourceE5EApplicationCreate,
ReadContext: resourceE5EApplicationRead,
UpdateContext: resourceE5EApplicationUpdate,
DeleteContext: resourceE5EApplicationDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "Application identifier.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Application name.",
},
},
}
}

func resourceE5EApplicationCreate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
a := apiFromProviderConfig(m)

application := e5ev1.Application{
Name: d.Get("name").(string),
}

if err := a.Create(ctx, &application); err != nil {
return diag.Errorf("failed to create resource: %s", err)
}

d.SetId(application.Identifier)

return resourceE5EApplicationRead(ctx, d, m)
}

func resourceE5EApplicationRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
a := apiFromProviderConfig(m)

application := e5ev1.Application{Identifier: d.Id()}
if err := a.Get(ctx, &application); api.IgnoreNotFound(err) != nil {
return diag.Errorf("failed getting resource: %s", err)
} else if err != nil {
d.SetId("")
return nil
}

var diags diag.Diagnostics

if err := d.Set("name", application.Name); err != nil {
diags = append(diags, diag.FromErr(err)...)
}

return diags
}

func resourceE5EApplicationUpdate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
a := apiFromProviderConfig(m)

application := e5ev1.Application{
Identifier: d.Id(),
Name: d.Get("name").(string),
}

if err := a.Update(ctx, &application); err != nil {
return diag.Errorf("failed updating resource: %s", err)
}

return resourceE5EApplicationRead(ctx, d, m)
}

func resourceE5EApplicationDelete(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
a := apiFromProviderConfig(m)

if err := a.Destroy(ctx, &e5ev1.Application{Identifier: d.Id()}); api.IgnoreNotFound(err) != nil {
return diag.Errorf("failed deleting resource %s", err)
}

return nil
}
Loading

0 comments on commit 1eb8516

Please sign in to comment.