File: account.rb

package info (click to toggle)
ruby-vagrant-cloud 3.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 408 kB
  • sloc: ruby: 4,343; makefile: 7
file content (112 lines) | stat: -rw-r--r-- 3,420 bytes parent folder | download | duplicates (2)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
module VagrantCloud
  # VagrantCloud account
  class Account
    # @return [Client]
    attr_reader :client
    # @return [String] username of this account
    attr_reader :username
    # @return [Instrumentor::Collection] Instrumentor in use
    attr_reader :instrumentor

    # Create a new Account instance
    #
    # @param [String] access_token Authentication token
    # @param [Client] client Client to use for account
    # @param [String] custom_server Custom server URL for client
    # @param [Integer] retry_count Number of retries on idempotent requests
    # @param [Integer] retry_interval Number of seconds to wait between requests
    # @param [Instrumentor::Core] instrumentor Instrumentor to use
    # @return [Account]
    def initialize(access_token: nil, client: nil, custom_server: nil, retry_count: nil, retry_interval: nil, instrumentor: nil)
      raise ArgumentError, "Account accepts `access_token` or `client` but not both" if
        client && access_token
      raise TypeError, "Expected `#{Client.name}` but received `#{client.class.name}`" if
        client && !client.is_a?(Client)

      if client
        @client = client
      else
        @client = Client.new(
          access_token: access_token,
          url_base: custom_server,
          retry_count: retry_count,
          retry_interval: retry_interval,
          instrumentor: instrumentor
        )
      end
      setup!
    end

    # @return [Search]
    def searcher
      Search.new(account: self)
    end

    #---------------------------
    # Authentication API Helpers
    #---------------------------

    # Create a new access token
    # @param [String] password Remote password
    # @param [String] description Description of token
    # @param [String] code 2FA code
    # @return [Response::CreateToken]
    def create_token(password:, description: Data::Nil, code: Data::Nil)
      r = client.authentication_token_create(username: username,
        password: password, description: description, code: code)

      Response::CreateToken.new(
        token: r[:token],
        token_hash: r[:token_hash],
        created_at: r[:created_at],
        description: r[:description]
      )
    end

    # Delete the current token
    #
    # @return [self]
    def delete_token
      client.authentication_token_delete
      self
    end

    # Validate the current token
    #
    # @return [self]
    def validate_token
      client.request(path: "authenticate")
      self
    end

    # Request a 2FA code is sent
    #
    # @param [String] delivery_method Delivery method of 2FA
    # @param [String] password Account password
    # @return [Response]
    def request_2fa_code(delivery_method:, password:)
      r = client.authentication_request_2fa_code(username: username,
        password: password, delivery_method: delivery_method)
      Response::Request2FA.new(destination: r.dig(:two_factor, :obfuscated_destination))
    end

    # Fetch the requested organization
    #
    # @param [String] name Organization name
    # @return [Organization]
    def organization(name: nil)
      org_name = name || username
      r = client.organization_get(name: org_name)
      Organization.load(account: self, **r)
    end

    protected

    def setup!
      if client.access_token
        r = client.request(path: "authenticate")
        @username = r.dig(:user, :username)
      end
    end
  end
end