File: resource_id.go

package info (click to toggle)
golang-github-tombuildsstuff-giovanni 0.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 15,908 kB
  • sloc: makefile: 3
file content (46 lines) | stat: -rw-r--r-- 1,295 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package filesystems

import (
	"fmt"
	"net/url"
	"strings"

	"github.com/tombuildsstuff/giovanni/storage/internal/endpoints"
)

// GetResourceID returns the Resource ID for the given Data Lake Storage FileSystem
// This can be useful when, for example, you're using this as a unique identifier
func (client Client) GetResourceID(accountName, shareName string) string {
	domain := endpoints.GetDataLakeStoreEndpoint(client.BaseURI, accountName)
	return fmt.Sprintf("%s/%s", domain, shareName)
}

type ResourceID struct {
	AccountName   string
	DirectoryName string
}

// ParseResourceID parses the specified Resource ID and returns an object
// which can be used to interact with the Data Lake Storage FileSystem API's
func ParseResourceID(id string) (*ResourceID, error) {
	// example: https://foo.dfs.core.windows.net/Bar
	if id == "" {
		return nil, fmt.Errorf("`id` was empty")
	}

	uri, err := url.Parse(id)
	if err != nil {
		return nil, fmt.Errorf("Error parsing ID as a URL: %s", err)
	}

	accountName, err := endpoints.GetAccountNameFromEndpoint(uri.Host)
	if err != nil {
		return nil, fmt.Errorf("Error parsing Account Name: %s", err)
	}

	directoryName := strings.TrimPrefix(uri.Path, "/")
	return &ResourceID{
		AccountName:   *accountName,
		DirectoryName: directoryName,
	}, nil
}