-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from volcengine/feat/ecs
Feat/ecs
- Loading branch information
Showing
26 changed files
with
2,005 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
data "volcengine_ecs_commands" "default" { | ||
command_id = "cmd-ychkepkhtim0tr3b****" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
135
volcengine/ecs/ecs_command/data_source_volcengine_ecs_commands.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
Oops, something went wrong.