-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Alibaba Cloud provider for add_cloud_metadata proccessor #4111
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package add_cloud_metadata | ||
|
||
import "github.com/elastic/beats/libbeat/common" | ||
|
||
// Alibaba Cloud Metadata Service | ||
// Document https://help.aliyun.com/knowledge_detail/49122.html | ||
func newAlibabaCloudMetadataFetcher(c common.Config) (*metadataFetcher, error) { | ||
ecsMetadataHost := "100.100.100.200" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's interesting that this is a fixed IP and not a domain name. Couldn't make much sense of the docs above unfortunately ;-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, maybe metadata is not a really important feature for Chinese users, the design looks not so serious in both Tencent Cloud and Alibaba Cloud :-)
You mean remove the document comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment is fine. |
||
ecsMetadataInstanceIDURI := "/latest/meta-data/instance-id" | ||
ecsMetadataRegionURI := "/latest/meta-data/region-id" | ||
ecsMetadataZoneURI := "/latest/meta-data/zone-id" | ||
|
||
ecsSchema := func(m map[string]interface{}) common.MapStr { | ||
return common.MapStr(m) | ||
} | ||
|
||
urls, err := getMetadataURLs(c, ecsMetadataHost, []string{ | ||
ecsMetadataInstanceIDURI, | ||
ecsMetadataRegionURI, | ||
ecsMetadataZoneURI, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
responseHandlers := map[string]responseHandler{ | ||
urls[0]: func(all []byte, result *result) error { | ||
result.metadata["instance_id"] = string(all) | ||
return nil | ||
}, | ||
urls[1]: func(all []byte, result *result) error { | ||
result.metadata["region"] = string(all) | ||
return nil | ||
}, | ||
urls[2]: func(all []byte, result *result) error { | ||
result.metadata["availability_zone"] = string(all) | ||
return nil | ||
}, | ||
} | ||
fetcher := &metadataFetcher{"ecs", nil, responseHandlers, ecsSchema} | ||
return fetcher, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package add_cloud_metadata | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/logp" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func initECSTestServer() *httptest.Server { | ||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.RequestURI == "/latest/meta-data/instance-id" { | ||
w.Write([]byte("i-wz9g2hqiikg0aliyun2b")) | ||
return | ||
} | ||
if r.RequestURI == "/latest/meta-data/region-id" { | ||
w.Write([]byte("cn-shenzhen")) | ||
return | ||
} | ||
if r.RequestURI == "/latest/meta-data/zone-id" { | ||
w.Write([]byte("cn-shenzhen-a")) | ||
return | ||
} | ||
|
||
http.Error(w, "not found", http.StatusNotFound) | ||
})) | ||
} | ||
|
||
func TestRetrieveAlibabaCloudMetadata(t *testing.T) { | ||
if testing.Verbose() { | ||
logp.LogInit(logp.LOG_DEBUG, "", false, true, []string{"*"}) | ||
} | ||
|
||
server := initECSTestServer() | ||
defer server.Close() | ||
|
||
config, err := common.NewConfigFrom(map[string]interface{}{ | ||
"host": server.Listener.Addr().String(), | ||
}) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
p, err := newCloudMetadata(*config) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
actual, err := p.Run(common.MapStr{}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
expected := common.MapStr{ | ||
"meta": common.MapStr{ | ||
"cloud": common.MapStr{ | ||
"provider": "ecs", | ||
"instance_id": "i-wz9g2hqiikg0aliyun2b", | ||
"region": "cn-shenzhen", | ||
"availability_zone": "cn-shenzhen-a", | ||
}, | ||
}, | ||
} | ||
assert.Equal(t, expected, actual) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realize that this is supposed to just be examples of the output, but it would be good to mention something like, "Only when VPC is selected as the network type of the instance, you can get the metadata."
@dedemorton Maybe you have wording or formatting suggestions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's change the wording because we don't want to take the text verbatim from the Alibaba docs. How about adding this before the example:
This metadata is only available when VPC is selected as the network type of the instance.
Or, assuming we are talking about an ECS instance here (apologies for my ignorance), this would be even better and less confusing for users:
This metadata is only available when VPC is selected as the network type of the ECS instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added on the position above the example output.