Method: SOULs::Sql#assign_ip

Defined in:
lib/souls/cli/gcloud/sql/index.rb

#assign_ipObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/souls/cli/gcloud/sql/index.rb', line 148

def assign_ip
  require(SOULs.get_mother_path.to_s + "/config/souls")
  project_id = SOULs.configuration.project_id
  instance_name = SOULs.configuration.instance_name
  ips = []
  ip =
    if options[:ip].blank?
      `curl inet-ip.info`.strip
    else
      options[:ip].strip
    end
  ips << ip
  cloud_sql = JSON.parse(
    `curl -X GET \
    -H "Authorization: Bearer "$(gcloud auth print-access-token) \
    "https://sqladmin.googleapis.com/v1/projects/#{project_id}/instances/#{instance_name}?fields=settings"`
  )
  begin
    cloud_sql["settings"]["ipConfiguration"]["authorizedNetworks"].blank?
    white_ips =
      cloud_sql["settings"]["ipConfiguration"]["authorizedNetworks"].map do |sql_ips|
        sql_ips["value"]
      end
    ips = (ips + white_ips).uniq
  rescue StandardError => e
    puts(e)
  end
  ips = ips.join(",")
  Whirly.start(spinner: "clock", interval: 420, stop: "🎉") do
    system(
      "
        gcloud sql instances patch #{instance_name} \
          --project=#{project_id} \
          --assign-ip \
          --authorized-networks=#{ips} \
          --quiet
        "
    )
    Whirly.status = Paint["Your IP is successfully added!", :green]
  end
  true
end