-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Alibaba Cloud provider for add_cloud_metadata proccessor (#4111)
* Support Alibaba Cloud provider for add_cloud_metadata processor * Add note for Alibaba Cloud provider in add_cloud_metadata processor documentation.
- Loading branch information
1 parent
24aed8c
commit 1344e4f
Showing
6 changed files
with
137 additions
and
1 deletion.
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
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
41 changes: 41 additions & 0 deletions
41
libbeat/processors/add_cloud_metadata/provider_alibaba_cloud.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,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" | ||
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 | ||
} |
69 changes: 69 additions & 0 deletions
69
libbeat/processors/add_cloud_metadata/provider_alibaba_cloud_test.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,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) | ||
} |
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