Class: Fastlane::Helper::OvoAzureHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/ovo_azure/helper/ovo_azure_helper.rb

Class Method Summary collapse

Class Method Details

.create_azure_uri(account:, container:, sas_token:, blob_path:) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/fastlane/plugin/ovo_azure/helper/ovo_azure_helper.rb', line 110

def self.create_azure_uri(account:, container:, sas_token:, blob_path:)
  hostname = "https://" +  + ".blob.core.windows.net"
  path = "/" + container + blob_path
  query = "?" + sas_token

  uri = URI(hostname + path + query)
  return uri
end

.delete_blob(account:, container:, sas_token:, blob_path:) ⇒ Object



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
# File 'lib/fastlane/plugin/ovo_azure/helper/ovo_azure_helper.rb', line 81

def self.delete_blob(account:, container:, sas_token:, blob_path:)
  uri = self.create_azure_uri(account: , container: container, sas_token: sas_token, blob_path: blob_path)

  UI.verbose("Starting delete of blob '" + blob_path + "' to Azure container..")

  begin
    headers = {
      "x-ms-date" => Time.new().strftime("%Y-%m-%dT%H:%M:%S%z")
    }

    request = Net::HTTP::Delete.new(uri, headers)
    response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
      http.request(request)
    end

    case response
    when Net::HTTPSuccess
      UI.verbose("Blob '" + blob_path + "' deleted successfully from Azure container.")
      return true
    else
      UI.error("An error occurred while deleting the blob '" + blob_path + "' from Azure container.: " + response.message)
      return nil
    end
  rescue => ex
    UI.error(ex.to_s) # it fails, however we don't want to fail everything just for this
    return nil
  end
end

.is_blob_exists(account:, container:, sas_token:, blob_path:) ⇒ Object



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
# File 'lib/fastlane/plugin/ovo_azure/helper/ovo_azure_helper.rb', line 10

def self.is_blob_exists(account:, container:, sas_token:, blob_path:)
  uri = self.create_azure_uri(account: , container: container, sas_token: sas_token, blob_path: blob_path)

  UI.verbose("Checking if blob '" + blob_path + "' exists in the Azure container...")

  begin
    request = Net::HTTP::Head.new(uri)
    response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
      http.request(request)
    end

    case response
    when Net::HTTPSuccess
      UI.verbose("Blob '" + blob_path + "' exists in the Azure container.")
      return true
    when Net::HTTPNotFound
      UI.verbose("Blob '" + blob_path + "' does not exist in the Azure container: " + response.message)
      return false
    else
      UI.error("An error occurred while checking if blob '" + blob_path + "' exists in the Azure container: " + response.message)
      return nil
    end
  rescue => ex
    UI.error(ex.to_s) # it fails, however we don't want to fail everything just for this
  end
end

.upload_blob(account:, container:, sas_token:, blob_path:, blob_content:, blob_cache_control:, blob_content_type:) ⇒ Object



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
# File 'lib/fastlane/plugin/ovo_azure/helper/ovo_azure_helper.rb', line 37

def self.upload_blob(account:, container:, sas_token:, blob_path:, blob_content:, blob_cache_control:, blob_content_type:)
  uri = self.create_azure_uri(account: , container: container, sas_token: sas_token, blob_path: blob_path)

  UI.verbose("Starting upload of blob '" + blob_path + "' to Azure container..")

  begin
    headers = {
      "x-ms-blob-content-disposition" => "attachment; filename=\"fname.ext\"",
      "x-ms-blob-type" => "BlockBlob",
      "x-ms-date" => Time.new().strftime("%Y-%m-%dT%H:%M:%S%z")
    }

    if blob_cache_control
      headers.store("x-ms-blob-cache-control", blob_cache_control)
    end

    request = Net::HTTP::Put.new(uri, headers)
    request.body = blob_content

    if blob_content_type
      request.content_type = blob_content_type
    end

    response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
      http.request(request)
    end

    case response
    when Net::HTTPSuccess
      UI.verbose("Blob '" + blob_path + "' uploaded successfully to Azure container.")
      return true
    when Net::HTTPNotFound
      UI.verbose("Blob '" + blob_path + "' does not exist in the Azure container: " + response.message)
      return false
    else
      UI.error("An error occurred while uploading the blob '" + blob_path + "' to Azure container.: " + response.message)
      return nil
    end
  rescue => ex
    UI.error(ex.to_s) # it fails, however we don't want to fail everything just for this
    return nil
  end
end