Skip to content

Commit

Permalink
feat(host):add State field
Browse files Browse the repository at this point in the history
  • Loading branch information
404tk committed Nov 11, 2024
1 parent 8da72af commit d7307b6
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 74 deletions.
File renamed without changes.
43 changes: 1 addition & 42 deletions pkg/providers/alibaba/sls/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,34 @@ package sls
import (
"fmt"
"net/http"
"os"
)

type Client struct {
accessKeyId string //Access Key Id
accessKeySecret string //Access Key Secret
securityToken string //sts token
debug bool
httpClient *http.Client
version string
internal bool
region string
endpoint string
}

func (client *Client) SetDebug(debug bool) {
client.debug = debug
}

// SetTransport sets transport to the http client
func (client *Client) SetTransport(transport http.RoundTripper) {
if client.httpClient == nil {
client.httpClient = &http.Client{}
}
client.httpClient.Transport = transport
}

const (
SLSDefaultEndpoint = "log.aliyuncs.com"
SLSAPIVersion = "0.6.0"
Version = "0.0.1"
)

// NewClient creates a new instance of ECS client
func NewClient(internal bool, region, accessKeyId, accessKeySecret, securityToken string) *Client {
endpoint := os.Getenv("SLS_ENDPOINT")
if endpoint == "" {
endpoint = SLSDefaultEndpoint
}
return NewClientWithEndpoint(endpoint, region, internal, accessKeyId, accessKeySecret, securityToken)
}

func NewClientForAssumeRole(internal bool, region, accessKeyId, accessKeySecret, securityToken string) *Client {
endpoint := os.Getenv("SLS_ENDPOINT")
if endpoint == "" {
endpoint = SLSDefaultEndpoint
}

return &Client{
accessKeyId: accessKeyId,
accessKeySecret: accessKeySecret,
securityToken: securityToken,
internal: internal,
region: region,
version: SLSAPIVersion,
endpoint: endpoint,
httpClient: &http.Client{},
}
}

func NewClientWithEndpoint(endpoint string, region string, internal bool, accessKeyId, accessKeySecret, securityToken string) *Client {
return &Client{
accessKeyId: accessKeyId,
accessKeySecret: accessKeySecret,
securityToken: securityToken,
internal: internal,
region: region,
version: SLSAPIVersion,
endpoint: endpoint,
endpoint: SLSDefaultEndpoint,
httpClient: &http.Client{},
}
}
Expand Down
33 changes: 3 additions & 30 deletions pkg/providers/alibaba/sls/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/http/httputil"
"net/url"
"runtime"
"strconv"
Expand Down Expand Up @@ -84,33 +82,19 @@ func (client *Client) doRequest(req *request) (*http.Response, error) {
}

hreq, err := http.NewRequest(req.method, req.url(), reader)
if err != nil {
return nil, err
}

for k, v := range req.headers {
if v != "" {
hreq.Header.Set(k, v)
}
}

if err != nil {
return nil, err
}
if client.debug {
reqDump, _ := httputil.DumpRequest(hreq, true)
log.Printf("---------------REQUEST---------------\n%s\n\n", string(reqDump))
}
t0 := time.Now()
resp, err := client.httpClient.Do(hreq)
t1 := time.Now()
if err != nil {
fmt.Println(err)
return nil, err
}
if client.debug {
resDump, _ := httputil.DumpResponse(resp, true)
log.Printf("---------------RESPONSE---------------\n%s\n\n", string(resDump))
log.Printf("Invoke %s %s %d (%v)", req.method, req.url(), resp.StatusCode, t1.Sub(t0))
}

if resp.StatusCode != 200 && resp.StatusCode != 204 && resp.StatusCode != 206 {
return nil, buildError(resp)
}
Expand All @@ -119,7 +103,6 @@ func (client *Client) doRequest(req *request) (*http.Response, error) {

func (client *Client) requestWithJsonResponse(req *request, v interface{}) error {
resp, err := client.doRequest(req)

if err != nil {
return err
}
Expand All @@ -132,16 +115,6 @@ func (client *Client) requestWithJsonResponse(req *request, v interface{}) error
return json.Unmarshal(data, v)
}

func (client *Client) requestWithClose(req *request) error {
resp, err := client.doRequest(req)
if err != nil {
return err
}

resp.Body.Close()
return nil
}

type Error struct {
StatusCode int
Code string `json:"errorCode,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions pkg/providers/aws/ec2/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (d *Driver) GetResource(ctx context.Context) ([]schema.Host, error) {
for _, instance := range reservation.Instances {
ip4 := aws.StringValue(instance.PublicIpAddress)
host := schema.Host{
State: instance.State.String(),
PublicIPv4: ip4,
PrivateIpv4: aws.StringValue(instance.PrivateIpAddress),
DNSName: aws.StringValue(instance.PublicDnsName),
Expand Down
5 changes: 3 additions & 2 deletions pkg/providers/azure/compute/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (d *Driver) GetResource(ctx context.Context) ([]schema.Host, error) {

for _, vm := range vmList {
_host := schema.Host{
State: vm.Status,
HostName: *vm.Name,
Region: *vm.Location,
}
Expand Down Expand Up @@ -98,7 +99,7 @@ func fetchResouceGroups(ctx context.Context, sess *Driver) (map[string][]string,
grClient.Authorizer = sess.Authorizer
resGrp[subscription] = []string{}

list, err := grClient.List(context.Background(), "", nil)
list, err := grClient.List(ctx, "", nil)
if err != nil {
return resGrp, err
}
Expand All @@ -112,7 +113,7 @@ func fetchResouceGroups(ctx context.Context, sess *Driver) (map[string][]string,
func fetchVMList(ctx context.Context, group, subscription string, auth autorest.Authorizer) ([]compute.VirtualMachine, error) {
vmClient := compute.NewVirtualMachinesClient(subscription)
vmClient.Authorizer = auth
vm, err := vmClient.List(context.Background(), group, "")
vm, err := vmClient.List(ctx, group, "")
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/providers/huawei/ecs/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (d *Driver) GetResource(ctx context.Context) ([]schema.Host, error) {
}
}
host := schema.Host{
State: instance.Status,
HostName: instance.Name,
PublicIPv4: ipv4,
PrivateIpv4: privateIPv4,
Expand Down
1 change: 1 addition & 0 deletions pkg/providers/tencent/cvm/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func (d *Driver) GetResource(ctx context.Context) ([]schema.Host, error) {
host := schema.Host{
HostName: *instance.InstanceName,
ID: *instance.InstanceId,
State: *instance.InstanceState,
PublicIPv4: ipv4,
PrivateIpv4: privateIPv4,
Public: ipv4 != "",
Expand Down
1 change: 1 addition & 0 deletions pkg/providers/tencent/lighthouse/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (d *Driver) GetResource(ctx context.Context) ([]schema.Host, error) {
_host := schema.Host{
HostName: *instance.InstanceName,
ID: *instance.InstanceId,
State: *instance.InstanceState,
PublicIPv4: ipv4,
PrivateIpv4: privateIPv4,
OSType: *instance.PlatformType, // LINUX_UNIX or WINDOWS
Expand Down
1 change: 1 addition & 0 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Resources struct {
type Host struct {
HostName string `table:"HostName"`
ID string `table:"Instance ID"`
State string `table:"State"`
PublicIPv4 string `table:"Public IP"`
PrivateIpv4 string `table:"Private IP"`
OSType string `table:"OS Type"`
Expand Down

0 comments on commit d7307b6

Please sign in to comment.