Skip to content

Commit

Permalink
Merge pull request #124 from volcengine/feat/ecs
Browse files Browse the repository at this point in the history
Feat/ecs
  • Loading branch information
zpp12354321 authored Sep 6, 2023
2 parents f4074d0 + f096372 commit edaa813
Show file tree
Hide file tree
Showing 26 changed files with 2,005 additions and 4 deletions.
2 changes: 1 addition & 1 deletion common/common_volcengine_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package common

const (
TerraformProviderName = "terraform-provider-volcengine"
TerraformProviderVersion = "0.0.105"
TerraformProviderVersion = "0.0.106"
)
3 changes: 3 additions & 0 deletions example/dataEcsCommands/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "volcengine_ecs_commands" "default" {
command_id = "cmd-ychkepkhtim0tr3b****"
}
4 changes: 4 additions & 0 deletions example/dataEcsInvocationResults/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "volcengine_ecs_invocation_results" "default" {
invocation_id = "ivk-ych9y4vujvl8j01c****"
invocation_result_status = ["Success"]
}
4 changes: 4 additions & 0 deletions example/dataEcsInvocations/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "volcengine_ecs_invocations" "default" {
invocation_id = "ivk-ych9y4vujvl8j01c****"
invocation_status = ["Success"]
}
8 changes: 8 additions & 0 deletions example/ecsCommand/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "volcengine_ecs_command" "foo" {
name = "tf-test"
description = "tf"
working_dir = "/home"
username = "root"
timeout = 100
command_content = "IyEvYmluL2Jhc2gKCgplY2hvICJvcGVyYXRpb24gc3VjY2VzcyEi"
}
13 changes: 13 additions & 0 deletions example/ecsInvocation/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "volcengine_ecs_invocation" "foo" {
command_id = "cmd-ychkepkhtim0tr3b****"
instance_ids = ["i-ychmz92487l8j00o****"]
invocation_name = "tf-test"
invocation_description = "tf"
username = "root"
timeout = 90
working_dir = "/home"
repeat_mode = "Rate"
frequency = "5m"
launch_time = "2023-06-20T09:48:00Z"
recurrence_end_time = "2023-06-20T09:59:00Z"
}
28 changes: 28 additions & 0 deletions sweep/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package sweep

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
ve "github.com/volcengine/terraform-provider-volcengine/common"
)

type sweepResource struct {
client *ve.SdkClient
resource *schema.Resource
resourceData *schema.ResourceData
}

func NewSweepResource(resource *schema.Resource, resourceData *schema.ResourceData, client *ve.SdkClient) *sweepResource {
return &sweepResource{
client: client,
resource: resource,
resourceData: resourceData,
}
}

func (s *sweepResource) Delete() error {
return s.resource.Delete(s.resourceData, s.client)
}

func (s *sweepResource) GetId() string {
return s.resourceData.Id()
}
127 changes: 127 additions & 0 deletions sweep/sweep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package sweep

import (
"fmt"
"os"
"strconv"
"strings"
"sync"

ve "github.com/volcengine/terraform-provider-volcengine/common"
"github.com/volcengine/terraform-provider-volcengine/logger"
)

func SharedClientForRegionWithResourceId(region string) (interface{}, error) {
var (
accessKey string
secretKey string
endpoint string
disableSSL bool
)

if accessKey = os.Getenv("VOLCENGINE_ACCESS_KEY"); accessKey == "" {
return nil, fmt.Errorf("%s can not be empty", "VOLCENGINE_ACCESS_KEY")
}
if secretKey = os.Getenv("VOLCENGINE_SECRET_KEY"); secretKey == "" {
return nil, fmt.Errorf("%s can not be empty", "VOLCENGINE_SECRET_KEY")
}
if endpoint = os.Getenv("VOLCENGINE_ENDPOINT"); endpoint == "" {
return nil, fmt.Errorf("%s can not be empty", "VOLCENGINE_ENDPOINT")
}
disableSSL, _ = strconv.ParseBool(os.Getenv("VOLCENGINE_DISABLE_SSL"))

config := ve.Config{
AccessKey: accessKey,
SecretKey: secretKey,
Region: region,
Endpoint: endpoint,
DisableSSL: disableSSL,
SessionToken: os.Getenv("VOLCENGINE_SESSION_TOKEN"),
ProxyUrl: os.Getenv("VOLCENGINE_PROXY_URL"),
CustomerHeaders: map[string]string{},
CustomerEndpoints: defaultCustomerEndPoints(),
}

headers := os.Getenv("VOLCENGINE_CUSTOMER_HEADERS")
if headers != "" {
hs1 := strings.Split(headers, ",")
for _, hh := range hs1 {
hs2 := strings.Split(hh, ":")
if len(hs2) == 2 {
config.CustomerHeaders[hs2[0]] = hs2[1]
}
}
}

endpoints := os.Getenv("VOLCENGINE_CUSTOMER_ENDPOINTS")
if endpoints != "" {
ends := strings.Split(endpoints, ",")
for _, end := range ends {
point := strings.Split(end, ":")
if len(point) == 2 {
config.CustomerEndpoints[point[0]] = point[1]
}
}
}

client, err := config.Client()
if err != nil {
return nil, err
}

return client, nil
}

func defaultCustomerEndPoints() map[string]string {
return map[string]string{
"veenedge": "veenedge.volcengineapi.com",
}
}

type SweeperInstance interface {
GetId() string
Delete() error
}

func SweeperScheduler(sweepInstances []SweeperInstance) error {
var (
wg sync.WaitGroup
syncMap sync.Map
errorStr string
)

if len(sweepInstances) == 0 {
return nil
}
wg.Add(len(sweepInstances))

for _, value := range sweepInstances {
sweepInstance := value
go func() {
defer func() {
if err := recover(); err != nil {
logger.DebugInfo(" Sweep Resource Panic, resource: ", sweepInstance.GetId(), "error: ", err)
}
wg.Done()
}()

err := sweepInstance.Delete()
if err != nil {
syncMap.Store(sweepInstance.GetId(), err)
}
}()
}

wg.Wait()
for _, sweepInstance := range sweepInstances {
if v, exist := syncMap.Load(sweepInstance.GetId()); exist {
if err, ok := v.(error); ok {
errorStr = errorStr + "Sweep Resource " + sweepInstance.GetId() + " error: " + err.Error() + ";\n"
}
}
}
if len(errorStr) > 0 {
return fmt.Errorf(errorStr)
}
return nil
}
13 changes: 13 additions & 0 deletions sweep/sweep_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package sweep_test

import (
"testing"

_ "github.com/volcengine/terraform-provider-volcengine/volcengine/vpc/vpc"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestMain(m *testing.M) {
resource.TestMain(m)
}
135 changes: 135 additions & 0 deletions volcengine/ecs/ecs_command/data_source_volcengine_ecs_commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package ecs_command

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
ve "github.com/volcengine/terraform-provider-volcengine/common"
)

func DataSourceVolcengineEcsCommands() *schema.Resource {
return &schema.Resource{
Read: dataSourceVolcengineEcsCommandsRead,
Schema: map[string]*schema.Schema{
"command_provider": {
Type: schema.TypeString,
Optional: true,
Description: "The provider of public command. When this field is not specified, query for custom commands.",
},
"command_id": {
Type: schema.TypeString,
Optional: true,
Description: "The id of ecs command.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of ecs command. This field support fuzzy query.",
},
"type": {
Type: schema.TypeString,
Optional: true,
Description: "The type of ecs command. Valid values: `Shell`.",
},
"order": {
Type: schema.TypeString,
Optional: true,
Description: "The order of ecs command query result.",
},
"name_regex": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsValidRegExp,
Description: "A Name Regex of Resource.",
},
"output_file": {
Type: schema.TypeString,
Optional: true,
Description: "File name where to save data source results.",
},
"total_count": {
Type: schema.TypeInt,
Computed: true,
Description: "The total count of query.",
},
"commands": {
Description: "The collection of query.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The id of the ecs command.",
},
"command_id": {
Type: schema.TypeString,
Computed: true,
Description: "The id of the ecs command.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the ecs command.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The create time of the ecs command.",
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: "The update time of the ecs command.",
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of the ecs command.",
},
"timeout": {
Type: schema.TypeInt,
Computed: true,
Description: "The timeout of the ecs command.",
},
"working_dir": {
Type: schema.TypeString,
Computed: true,
Description: "The working directory of the ecs command.",
},
"username": {
Type: schema.TypeString,
Computed: true,
Description: "The username of the ecs command.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "The description of the ecs command.",
},
"command_provider": {
Type: schema.TypeString,
Computed: true,
Description: "The provider of the public command.",
},
"command_content": {
Type: schema.TypeString,
Computed: true,
Description: "The base64 encoded content of the ecs command.",
},
"invocation_times": {
Type: schema.TypeInt,
Computed: true,
Description: "The invocation times of the ecs command. Public commands do not display the invocation times.",
},
},
},
},
},
}
}

func dataSourceVolcengineEcsCommandsRead(d *schema.ResourceData, meta interface{}) error {
service := NewEcsCommandService(meta.(*ve.SdkClient))
return ve.DefaultDispatcher().Data(service, d, DataSourceVolcengineEcsCommands())
}
Loading

0 comments on commit edaa813

Please sign in to comment.