forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add huaweicloud metadata support. (elastic#27607)
Signed-off-by: colstuwjx <colstuwjx@gmail.com> Co-authored-by: Jaime Soriano Pastor <jaime.soriano@elastic.co>
- Loading branch information
1 parent
f6174e8
commit 3bf0875
Showing
5 changed files
with
195 additions
and
0 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
80 changes: 80 additions & 0 deletions
80
libbeat/processors/add_cloud_metadata/provider_huawei_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,80 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package add_cloud_metadata | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/elastic/beats/v7/libbeat/common" | ||
) | ||
|
||
type hwMeta struct { | ||
ImageName string `json:"image_name"` | ||
VpcID string `json:"vpc_id"` | ||
} | ||
|
||
type hwMetadata struct { | ||
UUID string `json:"uuid"` | ||
AvailabilityZone string `json:"availability_zone"` | ||
RegionID string `json:"region_id"` | ||
Meta *hwMeta `json:"meta"` | ||
ProjectID string `json:"project_id"` | ||
Name string `json:"name"` | ||
} | ||
|
||
// Huawei Cloud Metadata Service | ||
// Document https://support.huaweicloud.com/usermanual-ecs/ecs_03_0166.html | ||
var huaweiMetadataFetcher = provider{ | ||
Name: "huawei-cloud", | ||
|
||
Local: true, | ||
|
||
Create: func(_ string, c *common.Config) (metadataFetcher, error) { | ||
metadataHost := "169.254.169.254" | ||
huaweiCloudMetadataJSONURI := "/openstack/latest/meta_data.json" | ||
|
||
huaweiCloudSchema := func(m map[string]interface{}) common.MapStr { | ||
m["service"] = common.MapStr{ | ||
"name": "ECS", | ||
} | ||
return common.MapStr{"cloud": m} | ||
} | ||
|
||
urls, err := getMetadataURLs(c, metadataHost, []string{ | ||
huaweiCloudMetadataJSONURI, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
responseHandlers := map[string]responseHandler{ | ||
urls[0]: func(all []byte, result *result) error { | ||
data := new(hwMetadata) | ||
err := json.Unmarshal(all, data) | ||
if err != nil { | ||
return err | ||
} | ||
result.metadata.Put("instance.id", data.UUID) | ||
result.metadata.Put("region", data.RegionID) | ||
result.metadata.Put("availability_zone", data.AvailabilityZone) | ||
return nil | ||
}, | ||
} | ||
fetcher := &httpMetadataFetcher{"huawei", nil, responseHandlers, huaweiCloudSchema} | ||
return fetcher, nil | ||
}, | ||
} |
97 changes: 97 additions & 0 deletions
97
libbeat/processors/add_cloud_metadata/provider_huawei_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,97 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package add_cloud_metadata | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/beats/v7/libbeat/beat" | ||
"github.com/elastic/beats/v7/libbeat/common" | ||
"github.com/elastic/beats/v7/libbeat/logp" | ||
) | ||
|
||
func initHuaweiCloudTestServer() *httptest.Server { | ||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.RequestURI == "/openstack/latest/meta_data.json" { | ||
w.Write([]byte(`{ | ||
"random_seed": "CWIZtYK4y5pzMtShTtCKx16qB1DsA/2kL0US4u1fHxedODNr7gos4RgdE/z9eHucnltnlJfDY1remfGL60yzTsvEIWPdECOpPaJm1edIYQaUvQzdeQwKcOQAHjUP5wLQzGA3j3Pw10p7u+M7glHEwNRoEY1WsbVYwzyOOkBnqb+MJ1aOhiRnfNtHOxjLNBSDvjHaQZzoHL+1YNAxDYFezE83nE2m3ciVwZO7xWpdKDQ+W5hYBUsYAWODRMOYqIR/5ZLsfAfxE2DhK+NvuMyJ5yjO+ObQf0DN5nRUSrM5ajs84UVMr9ylJuT78ckh83CLSttsjzXJ+sr07ZFsB6/6NABzziFL7Xn8z/mEBVmFXBiBgg7KcWSoH756w42VSdUezwTy9lW0spRmdvNBKV/PzrYyy0FMiGXXZwMOCyBD05CBRJlsPorwxZLlfRVmNvsTuMYB8TG3UUbFhoR8Bd5en+EC3ncH3QIUDWn0oVg28BVjWe5rADVQLX1h83ti6GD08YUGaxoNPXnJLZfiaucSacby2mG31xysxd8Tg0qPRq7744a1HPVryuauWR9pF0+qDmtskhenxK0FR+TQ4w0fRxTigteBsXx1pQu0iz+B8rP68uokU2faCC2IMHY2Tf9RPCe6Eef0/DdQhBft88PuJLwq52o/0qZ/n9HFL6LdgCU=", | ||
"uuid": "37da9890-8289-4c58-ba34-a8271c4a8216", | ||
"availability_zone": "cn-east-2b", | ||
"enterprise_project_id": "0", | ||
"launch_index": 0, | ||
"instance_type": "c3.large.2", | ||
"meta": { | ||
"os_bit": "64", | ||
"image_name": "CentOS 7.4", | ||
"vpc_id": "6dad7f50-db1d-4cce-b095-d27bc837d4bb" | ||
}, | ||
"region_id": "cn-east-2", | ||
"project_id": "c09b8baf28b845a9b53ed37575cfd61f", | ||
"name": "hwdev-test-1" | ||
}`)) | ||
return | ||
} | ||
|
||
http.Error(w, "not found", http.StatusNotFound) | ||
})) | ||
} | ||
|
||
func TestRetrieveHuaweiCloudMetadata(t *testing.T) { | ||
logp.TestingSetup() | ||
|
||
server := initHuaweiCloudTestServer() | ||
defer server.Close() | ||
|
||
config, err := common.NewConfigFrom(map[string]interface{}{ | ||
"providers": []string{"huawei"}, | ||
"host": server.Listener.Addr().String(), | ||
}) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
p, err := New(config) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
actual, err := p.Run(&beat.Event{Fields: common.MapStr{}}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
expected := common.MapStr{ | ||
"cloud": common.MapStr{ | ||
"provider": "huawei", | ||
"instance": common.MapStr{ | ||
"id": "37da9890-8289-4c58-ba34-a8271c4a8216", | ||
}, | ||
"region": "cn-east-2", | ||
"availability_zone": "cn-east-2b", | ||
"service": common.MapStr{ | ||
"name": "ECS", | ||
}, | ||
}, | ||
} | ||
assert.Equal(t, expected, actual.Fields) | ||
} |
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