mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-03-21 15:45:01 +00:00
lib/promscrape/discovery/yandexcloud: further code cleanup after 83a4abda3f
This commit is contained in:
parent
df34f8c5b8
commit
17290a4598
3 changed files with 109 additions and 167 deletions
|
@ -206,15 +206,15 @@ func (cfg *apiConfig) getServiceEndpoints(apiEndpoint string) (map[string]string
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type endpoints struct {
|
||||||
|
Endpoints []endpoint `json:"endpoints"`
|
||||||
|
}
|
||||||
|
|
||||||
type endpoint struct {
|
type endpoint struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Address string `json:"address"`
|
Address string `json:"address"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type endpoints struct {
|
|
||||||
Endpoints []endpoint `json:"endpoints"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// getAPIResponse calls Yandex Cloud apiURL and returns response body.
|
// getAPIResponse calls Yandex Cloud apiURL and returns response body.
|
||||||
func getAPIResponse(apiURL string, cfg *apiConfig) ([]byte, error) {
|
func getAPIResponse(apiURL string, cfg *apiConfig) ([]byte, error) {
|
||||||
creds, err := cfg.getFreshAPICredentials()
|
creds, err := cfg.getFreshAPICredentials()
|
||||||
|
|
|
@ -1,155 +0,0 @@
|
||||||
package yandexcloud
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type organization struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
ID string `json:"id"`
|
|
||||||
Labels map[string]string `json:"labels"`
|
|
||||||
Title string `json:"title"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type organizationsPage struct {
|
|
||||||
Organizations []organization `json:"organizations"`
|
|
||||||
NextPageToken string `json:"nextPageToken"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// See https://cloud.yandex.com/en-ru/docs/organization/api-ref/Organization/list
|
|
||||||
func parseOrganizationsPage(data []byte) (*organizationsPage, error) {
|
|
||||||
var page organizationsPage
|
|
||||||
if err := json.Unmarshal(data, &page); err != nil {
|
|
||||||
return nil, fmt.Errorf("cannot parse organizations page: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if page.Organizations == nil {
|
|
||||||
page.Organizations = make([]organization, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &page, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type cloud struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
ID string `json:"id"`
|
|
||||||
Labels map[string]string `json:"labels"`
|
|
||||||
OrganizationID string `json:"organizationId"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type cloudsPage struct {
|
|
||||||
Clouds []cloud `json:"clouds"`
|
|
||||||
NextPageToken string `json:"nextPageToken"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// See https://cloud.yandex.com/en-ru/docs/resource-manager/api-ref/Cloud/list
|
|
||||||
func parseCloudsPage(data []byte) (*cloudsPage, error) {
|
|
||||||
var page cloudsPage
|
|
||||||
if err := json.Unmarshal(data, &page); err != nil {
|
|
||||||
return nil, fmt.Errorf("cannot parse clouds page: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if page.Clouds == nil {
|
|
||||||
page.Clouds = make([]cloud, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &page, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type folder struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
ID string `json:"id"`
|
|
||||||
CloudID string `json:"cloudId"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
Status string `json:"status"`
|
|
||||||
Labels map[string]string `json:"labels"`
|
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type foldersPage struct {
|
|
||||||
Folders []folder `json:"folders"`
|
|
||||||
NextPageToken string `json:"nextPageToken"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// See https://cloud.yandex.com/en-ru/docs/resource-manager/api-ref/Folder/list
|
|
||||||
func parseFoldersPage(data []byte) (*foldersPage, error) {
|
|
||||||
var page foldersPage
|
|
||||||
if err := json.Unmarshal(data, &page); err != nil {
|
|
||||||
return nil, fmt.Errorf("cannot parse folders page: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if page.Folders == nil {
|
|
||||||
page.Folders = make([]folder, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &page, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type dnsRecord struct {
|
|
||||||
FQDN string `json:"fqdn"`
|
|
||||||
DNSZoneID string `json:"dnsZoneId"`
|
|
||||||
TTL string `json:"ttl"`
|
|
||||||
PTR bool `json:"ptr"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type oneToOneNat struct {
|
|
||||||
Address string `json:"address"`
|
|
||||||
IPVersion string `json:"ipVersion"`
|
|
||||||
DNSRecords []dnsRecord `json:"dnsRecords"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type primaryV4Address struct {
|
|
||||||
Address string `json:"address"`
|
|
||||||
OneToOneNat oneToOneNat `json:"oneToOneNat"`
|
|
||||||
DNSRecords []dnsRecord `json:"dnsRecords"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type networkInterface struct {
|
|
||||||
Index string `json:"index"`
|
|
||||||
MacAddress string `json:"macAddress"`
|
|
||||||
SubnetID string `json:"subnetId"`
|
|
||||||
PrimaryV4Address primaryV4Address `json:"primaryV4Address"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type resources struct {
|
|
||||||
Cores string `json:"cores"`
|
|
||||||
CoreFraction string `json:"coreFraction"`
|
|
||||||
Memory string `json:"memory"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type instance struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
FQDN string `json:"fqdn"`
|
|
||||||
Status string `json:"status"`
|
|
||||||
FolderID string `json:"folderId"`
|
|
||||||
PlatformID string `json:"platformId"`
|
|
||||||
Resources resources `json:"resources"`
|
|
||||||
NetworkInterfaces []networkInterface `json:"networkInterfaces"`
|
|
||||||
Labels map[string]string `json:"labels,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type instancesPage struct {
|
|
||||||
Instances []instance `json:"instances"`
|
|
||||||
NextPageToken string `json:"nextPageToken"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// See https://cloud.yandex.com/en-ru/docs/compute/api-ref/Instance/list
|
|
||||||
func parseInstancesPage(data []byte) (*instancesPage, error) {
|
|
||||||
var page instancesPage
|
|
||||||
if err := json.Unmarshal(data, &page); err != nil {
|
|
||||||
return nil, fmt.Errorf("cannot parse instances page: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if page.Instances == nil {
|
|
||||||
page.Instances = make([]instance, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &page, nil
|
|
||||||
}
|
|
|
@ -1,6 +1,7 @@
|
||||||
package yandexcloud
|
package yandexcloud
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -46,8 +47,8 @@ func (cfg *apiConfig) getInstances(folderID string) ([]instance, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot get instances: %w", err)
|
return nil, fmt.Errorf("cannot get instances: %w", err)
|
||||||
}
|
}
|
||||||
ip, err := parseInstancesPage(data)
|
var ip instancesPage
|
||||||
if err != nil {
|
if err := json.Unmarshal(data, &ip); err != nil {
|
||||||
return nil, fmt.Errorf("cannot parse instances response from %q: %w; response body: %s", nextLink, err, data)
|
return nil, fmt.Errorf("cannot parse instances response from %q: %w; response body: %s", nextLink, err, data)
|
||||||
}
|
}
|
||||||
instances = append(instances, ip.Instances...)
|
instances = append(instances, ip.Instances...)
|
||||||
|
@ -58,6 +59,56 @@ func (cfg *apiConfig) getInstances(folderID string) ([]instance, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// See https://cloud.yandex.com/en-ru/docs/compute/api-ref/Instance/list
|
||||||
|
type instancesPage struct {
|
||||||
|
Instances []instance `json:"instances"`
|
||||||
|
NextPageToken string `json:"nextPageToken"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type instance struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
FQDN string `json:"fqdn"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
FolderID string `json:"folderId"`
|
||||||
|
PlatformID string `json:"platformId"`
|
||||||
|
Resources resources `json:"resources"`
|
||||||
|
NetworkInterfaces []networkInterface `json:"networkInterfaces"`
|
||||||
|
Labels map[string]string `json:"labels,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type resources struct {
|
||||||
|
Cores string `json:"cores"`
|
||||||
|
CoreFraction string `json:"coreFraction"`
|
||||||
|
Memory string `json:"memory"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type networkInterface struct {
|
||||||
|
Index string `json:"index"`
|
||||||
|
MacAddress string `json:"macAddress"`
|
||||||
|
SubnetID string `json:"subnetId"`
|
||||||
|
PrimaryV4Address primaryV4Address `json:"primaryV4Address"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type primaryV4Address struct {
|
||||||
|
Address string `json:"address"`
|
||||||
|
OneToOneNat oneToOneNat `json:"oneToOneNat"`
|
||||||
|
DNSRecords []dnsRecord `json:"dnsRecords"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type oneToOneNat struct {
|
||||||
|
Address string `json:"address"`
|
||||||
|
IPVersion string `json:"ipVersion"`
|
||||||
|
DNSRecords []dnsRecord `json:"dnsRecords"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type dnsRecord struct {
|
||||||
|
FQDN string `json:"fqdn"`
|
||||||
|
DNSZoneID string `json:"dnsZoneId"`
|
||||||
|
TTL string `json:"ttl"`
|
||||||
|
PTR bool `json:"ptr"`
|
||||||
|
}
|
||||||
|
|
||||||
func (cfg *apiConfig) getFolders(clouds []cloud) ([]folder, error) {
|
func (cfg *apiConfig) getFolders(clouds []cloud) ([]folder, error) {
|
||||||
foldersURL := cfg.serviceEndpoints["resource-manager"] + "/resource-manager/v1/folders"
|
foldersURL := cfg.serviceEndpoints["resource-manager"] + "/resource-manager/v1/folders"
|
||||||
var folders []folder
|
var folders []folder
|
||||||
|
@ -69,8 +120,8 @@ func (cfg *apiConfig) getFolders(clouds []cloud) ([]folder, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot get folders: %w", err)
|
return nil, fmt.Errorf("cannot get folders: %w", err)
|
||||||
}
|
}
|
||||||
fp, err := parseFoldersPage(data)
|
var fp foldersPage
|
||||||
if err != nil {
|
if err := json.Unmarshal(data, &fp); err != nil {
|
||||||
return nil, fmt.Errorf("cannot parse folders response from %q: %w; response body: %s", nextLink, err, data)
|
return nil, fmt.Errorf("cannot parse folders response from %q: %w; response body: %s", nextLink, err, data)
|
||||||
}
|
}
|
||||||
folders = append(folders, fp.Folders...)
|
folders = append(folders, fp.Folders...)
|
||||||
|
@ -83,6 +134,22 @@ func (cfg *apiConfig) getFolders(clouds []cloud) ([]folder, error) {
|
||||||
return folders, nil
|
return folders, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// See https://cloud.yandex.com/en-ru/docs/resource-manager/api-ref/Folder/list
|
||||||
|
type foldersPage struct {
|
||||||
|
Folders []folder `json:"folders"`
|
||||||
|
NextPageToken string `json:"nextPageToken"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type folder struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
CloudID string `json:"cloudId"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
Labels map[string]string `json:"labels"`
|
||||||
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
|
}
|
||||||
|
|
||||||
func (cfg *apiConfig) getClouds(orgs []organization) ([]cloud, error) {
|
func (cfg *apiConfig) getClouds(orgs []organization) ([]cloud, error) {
|
||||||
cloudsURL := cfg.serviceEndpoints["resource-manager"] + "/resource-manager/v1/clouds"
|
cloudsURL := cfg.serviceEndpoints["resource-manager"] + "/resource-manager/v1/clouds"
|
||||||
if len(orgs) == 0 {
|
if len(orgs) == 0 {
|
||||||
|
@ -102,8 +169,8 @@ func (cfg *apiConfig) getClouds(orgs []organization) ([]cloud, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot get clouds: %w", err)
|
return nil, fmt.Errorf("cannot get clouds: %w", err)
|
||||||
}
|
}
|
||||||
cp, err := parseCloudsPage(data)
|
var cp cloudsPage
|
||||||
if err != nil {
|
if err := json.Unmarshal(data, &cp); err != nil {
|
||||||
return nil, fmt.Errorf("cannot parse clouds response from %q: %w; response body: %s", nextLink, err, data)
|
return nil, fmt.Errorf("cannot parse clouds response from %q: %w; response body: %s", nextLink, err, data)
|
||||||
}
|
}
|
||||||
clouds = append(clouds, cp.Clouds...)
|
clouds = append(clouds, cp.Clouds...)
|
||||||
|
@ -116,6 +183,21 @@ func (cfg *apiConfig) getClouds(orgs []organization) ([]cloud, error) {
|
||||||
return clouds, nil
|
return clouds, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// See https://cloud.yandex.com/en-ru/docs/resource-manager/api-ref/Cloud/list
|
||||||
|
type cloudsPage struct {
|
||||||
|
Clouds []cloud `json:"clouds"`
|
||||||
|
NextPageToken string `json:"nextPageToken"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type cloud struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
Labels map[string]string `json:"labels"`
|
||||||
|
OrganizationID string `json:"organizationId"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
|
}
|
||||||
|
|
||||||
func (cfg *apiConfig) getOrganizations() ([]organization, error) {
|
func (cfg *apiConfig) getOrganizations() ([]organization, error) {
|
||||||
orgsURL := cfg.serviceEndpoints["organization-manager"] + "/organization-manager/v1/organizations"
|
orgsURL := cfg.serviceEndpoints["organization-manager"] + "/organization-manager/v1/organizations"
|
||||||
var orgs []organization
|
var orgs []organization
|
||||||
|
@ -125,8 +207,8 @@ func (cfg *apiConfig) getOrganizations() ([]organization, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot get organizations: %w", err)
|
return nil, fmt.Errorf("cannot get organizations: %w", err)
|
||||||
}
|
}
|
||||||
op, err := parseOrganizationsPage(data)
|
var op organizationsPage
|
||||||
if err != nil {
|
if err := json.Unmarshal(data, &op); err != nil {
|
||||||
return nil, fmt.Errorf("cannot parse organizations response from %q: %w; response body: %s", nextLink, err, data)
|
return nil, fmt.Errorf("cannot parse organizations response from %q: %w; response body: %s", nextLink, err, data)
|
||||||
}
|
}
|
||||||
orgs = append(orgs, op.Organizations...)
|
orgs = append(orgs, op.Organizations...)
|
||||||
|
@ -136,3 +218,18 @@ func (cfg *apiConfig) getOrganizations() ([]organization, error) {
|
||||||
nextLink = orgsURL + "&pageToken=" + url.QueryEscape(op.NextPageToken)
|
nextLink = orgsURL + "&pageToken=" + url.QueryEscape(op.NextPageToken)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// See https://cloud.yandex.com/en-ru/docs/organization/api-ref/Organization/list
|
||||||
|
type organizationsPage struct {
|
||||||
|
Organizations []organization `json:"organizations"`
|
||||||
|
NextPageToken string `json:"nextPageToken"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type organization struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
Labels map[string]string `json:"labels"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue