0

我无法获取 Azure 容器注册表的特定容器存储库的属性。

我也尝试过奇怪的组合来运行它。

尝试 1

loginURI := "https://management.azure.com/subscriptions/" + subscriptionId + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.ContainerRegistry/registries/" + registryName + ".azurecr.io"

尝试 1 条错误消息

azure: Service returned an error. Status=400 Code="MissingApiVersionParameter" Message="The api-version query parameter (?api-version=) is required for all requests."

尝试 2

loginURI := "https://" + registryName + ".azurecr.io/acr/v1/hello-world"

尝试 3

loginURI := "https://management.azure.com/subscriptions/" + subscriptionId + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.ContainerRegistry/registries/" + registryName + ".azurecr.io?api-version=2019-08-15-preview"

尝试 3 错误消息

2020/04/28 13:33:45 Error while fetching location list, containerregistry.RepositoryClient#GetAttributes: Failure responding to request: StatusCode=400 -- Original Error: autorest/
azure: Service returned an error. Status=400 Code="NoRegisteredProviderFound" Message="No registered resource provider found for location 'westus' and API version '2019-08-15-previ
ew/acr/v1/hello-world?api-version=2019-08-15-preview' for type 'registries'. The supported api-versions are '2016-06-27-preview, 2017-03-01, 2017-10-01, 2019-05-01, 2019-12-01-prev
iew, 2017-06-01-preview'. The supported locations are 'westus, eastus, southcentralus, westeurope, northeurope, uksouth, ukwest, australiaeast, australiasoutheast, centralindia, ko
reacentral, francecentral, southafricanorth, uaenorth, eastasia, japaneast, japanwest, southeastasia, southindia, brazilsouth, canadaeast, canadacentral, centralus, eastus2, northc
entralus, westcentralus, westus2, switzerlandnorth'."

但是在获取属性时仍然没有运气

如果我在这里做错了什么,任何人都可以建议纠正我吗?这是为容器存储库客户端提供 loginURI 的正确方法吗?我们是否需要指定 ApiVersion 相同,如果是,那么如何?是否有任何可用于 ACR 存储库的示例,以便我参考。

示例代码可以在这里找到

Go 版本 go1.14.2 windows/amd64

package main

import (
    "context"
    "fmt"
    "github.com/Azure/azure-sdk-for-go/profiles/preview/preview/containerregistry/runtime/containerregistry"
    "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2015-11-01/subscriptions"
    "github.com/Azure/go-autorest/autorest/azure/auth"
    "log"
    "os"
)
func main() {
    subscriptionId := ""
    clientId := ""
    clientSecret := ""
    tenantId := ""
    resourceGroupName := ""
    registryName := "test"
        repositoryName := "hello-world"
    err := os.Setenv("AZURE_CLIENT_ID", clientId)
    if err != nil {
        log.Printf("Error while setting env variable, %v ", err)
    }
    err = os.Setenv("AZURE_CLIENT_SECRET", clientSecret)
    if err != nil {
        log.Printf("Error while setting env variable, %v ", err)

    }
    err = os.Setenv("AZURE_TENANT_ID", tenantId)
    if err != nil {
        log.Printf("Error while setting env variable, %v ", err)

    }
    err = os.Setenv("AZURE_SUBSCRIPTION_ID", subscriptionId)
    if err != nil {
        log.Printf("Error while setting env variable, %v ", err)
    }
    authorizer, err := auth.NewAuthorizerFromEnvironment()
    if err != nil {
        log.Printf("Error while creating an new authentication, %v ", err)

    }
    loginURI := "https://management.azure.com/subscriptions/" + subscriptionId + "/resourceGroups/" + resourceGroupName + "/providers/Microsoft.ContainerRegistry/registries/"
    subscriptionsClient := containerregistry.NewRepositoryClient(loginURI)
    subscriptionsClient.Authorizer = authorizer
    attributes, err2 := subscriptionsClient.GetAttributes(context.Background(), registryName+ "/"+ repositoryName)
    if err2 != nil {
        log.Printf("Error while fetching attributes, %v ", err)
    }
    fmt.Print(attributes)
}
4

1 回答 1

0

得到了答案

https://github.com/Azure/azure-sdk-for-go/issues/8654

您所要做的就是使用这样的 AZURE 凭据,

authorizer, err := auth.NewAuthorizerFromEnvironment()
containerRegistryClient := containerregistry.NewRegistriesClient(credential.SubscriptionId)
containerRegistryClient.Authorizer = authorizer
containerRegistryCredential, err := containerRegistryClient.ListCredentials(context.Background(), resourceGroupName, registryName)

之后,您将收到访问容器存储库的凭据,现在使用基本授权器进行身份验证,

basicAuthorizer := autorest.NewBasicAuthorizer(<userName>, <password>)
tagClient := repository.NewTagClient("https://" + <loginServer>)
tagClient.Authorizer = basicAuthorizer
repositoryTagDetails, err := tagClient.GetList(context.Background(), <repositoryName>, "", nil, "", "")

而已!

谢谢

于 2020-11-28T20:44:58.593 回答