Class: PuppetX::Puppetlabs::CD4PEClient

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/puppetlabs/cd4pe_client.rb

Overview

Provides a class for interacting with CD4PE's API

Constant Summary collapse

LOGIN_ENDPOINT =
'/login'.freeze
ROOT_AJAX_ENDPOINT =
'/root/ajax'.freeze
ROOT_ENDPOINT_SETTINGS =
'/root/endpoint-settings'.freeze
ROOT_STORAGE_SETTINGS =
'/root/storage-settings'.freeze
SIGNUP_ENDPOINT =
'/signup'.freeze
HW_CONFIG_ENDPOINT =
'/root/hw-config'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, email = nil, password = nil, base64_cacert = nil, insecure_https = false) ⇒ CD4PEClient

Returns a new instance of CD4PEClient.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 19

def initialize(hostname, email = nil, password = nil, base64_cacert = nil, insecure_https = false)
  uri = URI.parse(hostname)

  @config = {
    server: uri.host,
    port: uri.port,
    scheme: uri.scheme || 'https',
    path: uri.path || '',
    base64_cacert: base64_cacert,
    insecure_https: insecure_https,
    email: email,
    password: password,
  }
  # Only set the cookie if creds are provided, otherwise, make unauthenticated requests
  if @config[:email] && @config[:password]
    set_cookie
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 10

def config
  @config
end

Returns the value of attribute cookie.



82
83
84
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 82

def cookie
  @cookie
end

Instance Method Details

#add_deployment_to_stage(workspace, repo_name, repo_type, branch_name, pe_creds_name, node_group_name, stage_name, add_stage_after, autopromote, trigger_condition) ⇒ Object



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 443

def add_deployment_to_stage(workspace,
                            repo_name,
                            repo_type,
                            branch_name,
                            pe_creds_name,
                            node_group_name,
                            stage_name,
                            add_stage_after,
                            autopromote,
                            trigger_condition)
  current_pipeline = get_pipeline_for_branch(workspace, repo_name, repo_type, branch_name)
  puppet_environment_res = list_puppet_environments(workspace, pe_creds_name)
  puppet_environments = JSON.parse(puppet_environment_res.body, symbolize_names: true)
  matched_environments = puppet_environments.select { |env| node_group_name.casecmp(env[:name]).zero? }
  if matched_environments.empty?
    raise Puppet::Error, "Could not find node group for name: #{node_group_name}"
  end
  if matched_environments.length > 1
    raise Puppet::Error, "Found multiple node groups for name: #{node_group_name}. Assign the node groups unique names and try again."
  end
  environment = matched_environments[0]
  new_deployment = {
    peModuleDeploymentTemplate: {
      settings: {
        doCodeDeploy: true,
        environment: {
          nodeGroupBranch: environment[:environment],
          nodeGroupId: environment[:id],
          nodeGroupName: environment[:name],
          peCredentialsId: {
            domain: current_pipeline[:projectId][:domain],
            name: pe_creds_name,
          },
        },
        moduleId: {
          domain: current_pipeline[:projectId][:domain],
          name: repo_name,
        },
      },
    },
  }
  new_stages = CD4PEPipelineUtils.add_destination_to_stage(current_pipeline[:stages], new_deployment, stage_name, add_stage_after, autopromote, trigger_condition)
  new_pipeline_res = upsert_pipeline_stages(workspace, repo_name, repo_type, current_pipeline[:id], new_stages)
  JSON.parse(new_pipeline_res.body, symbolize_names: true)
end

#add_job_to_stage(workspace, repo_name, repo_type, branch_name, job_name, stage_name, add_stage_after, autopromote, trigger_condition) ⇒ Object



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 489

def add_job_to_stage(workspace,
                     repo_name,
                     repo_type,
                     branch_name,
                     job_name,
                     stage_name,
                     add_stage_after,
                     autopromote,
                     trigger_condition)
  current_pipeline = get_pipeline_for_branch(workspace, repo_name, repo_type, branch_name)
  job_template_res = list_job_templates(workspace)
  job_templates = JSON.parse(job_template_res.body, symbolize_names: true)

  matched_job_templates = job_templates[:rows].select { |template| job_name.casecmp(template[:name]).zero? }
  if matched_job_templates.empty?
    raise Puppet::Error, "Could not find job for name: #{job_name}"
  end
  if matched_job_templates.length > 1
    raise Puppet::Error, "Found multiple jobs for name: #{job_name}. Give the job a unique name and try again."
  end
  new_job_destination = { vmJobTemplateId: matched_job_templates[0][:id] }
  new_stages = CD4PEPipelineUtils.add_destination_to_stage(current_pipeline[:stages], new_job_destination, stage_name, add_stage_after, autopromote, trigger_condition)
  new_pipeline_res = upsert_pipeline_stages(workspace, repo_name, repo_type, current_pipeline[:id], new_stages)
  JSON.parse(new_pipeline_res.body, symbolize_names: true)
end

#add_oauth_integration(provider, client_id, client_secret) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 177

def add_oauth_integration(provider, client_id, client_secret)
  payload = {
    op: 'AddOauthIntegration',
    content: {
      provider: provider,
      publicKey: client_id,
      privateKey: client_secret,
    },
  }
  make_request(:post, ROOT_AJAX_ENDPOINT, payload.to_json)
end

#add_pr_gate_to_stage(workspace, repo_name, repo_type, branch_name, stage_name) ⇒ Object



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 515

def add_pr_gate_to_stage(workspace, repo_name, repo_type, branch_name, stage_name)
  current_pipeline = get_pipeline_for_branch(workspace, repo_name, repo_type, branch_name)
  current_stages = current_pipeline[:stages]
  existing_stage_idx = CD4PEPipelineUtils.get_stage_index_by_name(current_stages, stage_name)
  current_stages[existing_stage_idx][:pipelineGate] = {
    projectPipelineGateType: 'PULLREQUEST',
    stageNum: current_stages[existing_stage_idx][:stageNum],
  }
  if current_stages[existing_stage_idx].key?(:triggerOn)
    current_stages[existing_stage_idx][:pipelineGate][:triggerOn] = current_stages[existing_stage_idx][:triggerOn]
  end
  if current_stages[existing_stage_idx].key?(:triggerCondition)
    current_stages[existing_stage_idx][:pipelineGate][:triggerCondition] = current_stages[existing_stage_idx][:triggerCondition]
  end

  upsert_pipeline_stages(workspace, repo_name, repo_type, current_pipeline[:id], current_stages)
  pr_build_triggers = ['Commit', 'PullRequest']
  build_trigger_res = set_pipeline_auto_build_triggers(workspace, repo_name, repo_type, current_pipeline[:id], current_pipeline[:name], pr_build_triggers)
  set_is_build_pr_allowed(workspace, repo_name, repo_type, false)
  JSON.parse(build_trigger_res.body, symbolize_names: true)
end

#add_repo(workspace, source_control, repo_org, source_repo_name, repo_name, repo_type) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 207

def add_repo(workspace, source_control, repo_org, source_repo_name, repo_name, repo_type)
  org_res = list_source_orgs(workspace, source_control)
  if org_res.code != '200'
    raise Puppet::Error, "Error while listing source orgs: #{org_res.body}"
  end

  source_orgs = JSON.parse(org_res.body, symbolize_names: true)
  matched_source_orgs = source_orgs.select { |org| repo_org.casecmp(org[:organization]).zero? }

  if matched_source_orgs.empty?
    raise Puppet::Error, "Could not find repo orgs for name: #{repo_org}"
  end
  if matched_source_orgs.length > 1
    raise Puppet::Error, "Found multiple repo orgs for name: #{repo_org}. Org names must be unique when referenced here."
  end

  repos_res = search_source_repos(workspace, source_control, matched_source_orgs[0], source_repo_name)
  if repos_res.code != '200'
    raise Puppet::Error, "Error while searching for repository: #{repos_res.body}"
  end
  source_repos = JSON.parse(repos_res.body, symbolize_names: true)
  if source_repos.empty?
    raise Puppet::Error, "Could not find source repo for name: #{source_repo_name}"
  end
  matched_source_repos = source_repos.select { |repo| source_repo_name.casecmp(repo[:repoName]).zero? }
  if matched_source_repos.length > 1
    raise Puppet::Error, "Found multiple repositories for repository name: #{repo_name}"
  end
  # There should only be one repo from the search
  source_repo = matched_source_repos[0]
  case repo_type
  when 'control'
    repo_op = 'CreateControlRepo'
  when 'module'
    repo_op = 'CreateModule'
  else
    raise Puppet::Error "repo_type does not match one of: 'control', 'module'"
  end

  payload = {
    op: repo_op,
    content: {
      name: repo_name,
      srcRepoDisplayName: source_repo[:repoDisplayName],
      srcRepoDisplayOwner: source_repo[:owner],
      srcRepoId: source_repo[:repoId],
      srcRepoName: source_repo[:repoName],
      srcRepoOwner: source_repo[:owner],
      # The search API returns the value of the provider as an lowercase string. It needs to be uppercase when creating a control repo.
      srcRepoProvider: source_repo[:provider].upcase,
    },
  }
  make_request(:post, get_ajax_endpoint(workspace), payload.to_json)
end

#add_vcs_integration(provider, workspace, provider_specific) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 126

def add_vcs_integration(provider, workspace, provider_specific)
  case provider
  when 'gitlab'
    op = 'ConnectGitLab'
  when 'GHE'
    op = 'ConnectGitHubEnterprise'
  when 'bbs'
    op = 'ConnectBitbucketServer'
  end

  payload = { op: op }
  payload['content'] = provider_specific

  make_request(:post, "/#{workspace}/ajax", payload.to_json)
end

#create_agent_credentialsObject



584
585
586
587
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 584

def create_agent_credentials
  endpoint = "#{HW_CONFIG_ENDPOINT}?op=CreateAgentCredentials"
  make_request(:get, endpoint)
end

#create_pipeline(workspace, repo_name, repo_branch, pipeline_type) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 318

def create_pipeline(workspace, repo_name, repo_branch, pipeline_type)
  # Default sources
  sources = [
    {
      autoBuildTriggers: ['Commit'],
      branch: repo_branch,
      containerName: repo_branch,
      trigger: 'SOURCE_REPOSITORY',
    },
  ]
  payload = {
    op: 'CreatePipeline',
    content: {
      pipelineName: repo_branch,
      sources: sources,
    },
  }

  if pipeline_type == 'control'
    payload[:content][:controlRepoName] = repo_name
  elsif pipeline_type == 'module'
    payload[:content][:moduleName] = repo_name
  end
  make_request(:post, get_ajax_endpoint(workspace), payload.to_json)
end

#create_root_accountObject



84
85
86
87
88
89
90
91
92
93
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 84

def 
  payload = {
    op: 'CreateRootAccount',
    content: {
      email: @config[:email],
      passwd: @config[:password],
    },
  }
  make_request(:post, '/root-account', payload.to_json)
end

#create_user(email, username, password, first_name, last_name, company_name) ⇒ Object



569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 569

def create_user(email, username, password, first_name, last_name, company_name)
  payload = {
    op: 'PfiSignup',
    content: {
      email: email,
      passwd: password,
      username: username,
      firstName: first_name,
      lastName: last_name,
      companyName: company_name,
    },
  }
  make_request(:post, SIGNUP_ENDPOINT, payload.to_json)
end

#create_workspace(workspace, for_user) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 116

def create_workspace(workspace, for_user)
  payload = {
    op: 'CreateWorkspace',
    content: {
      workspaceName: workspace,
    },
  }
  make_request(:post, "/#{for_user}/ajax", payload.to_json)
end

#discover_pe_credentials(workspace, creds_name, pe_username, pe_password, pe_token, pe_console_host, token_lifetime) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 189

def discover_pe_credentials(workspace, creds_name, pe_username, pe_password, pe_token, pe_console_host, token_lifetime)
  payload = {
    op: 'DiscoverPuppetEnterpriseCredentials',
    content: {
      consoleHost: pe_console_host,
      name: creds_name,
      username: pe_username,
      password: pe_password,
      token: pe_token,
      lifetime: token_lifetime,
      puppetServerCertificate: '',
      puppetServerEndpoint: '',
      puppetServerPrivateKey: '',
    },
  }
  make_request(:post, get_ajax_endpoint(workspace), payload.to_json)
end

#generate_trial_licenseObject



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 103

def generate_trial_license
  # generate the trial license
  response = make_request(:get, '/generate-trial-license?op=GenerateTrialLicense')
  license = JSON.parse(response.body)

  # save the trial license
  payload = {
    op: 'SavePfiLicense', # Note: endpoint is different from the save_license endpoint
    content: license,
  }
  make_request(:post, '/root/license', payload.to_json)
end

#get_ajax_endpoint(workspace) ⇒ Object



38
39
40
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 38

def get_ajax_endpoint(workspace)
  "/#{workspace}/ajax"
end

#get_pipeline_for_branch(workspace, repo_name, pipeline_type, branch_name) ⇒ Object



430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 430

def get_pipeline_for_branch(workspace, repo_name, pipeline_type, branch_name)
  pipelines_by_name_res = list_pipelines_by_name(workspace, repo_name, pipeline_type, branch_name)
  pipelines_by_name = JSON.parse(pipelines_by_name_res.body, symbolize_names: true)
  matched_pipelines = pipelines_by_name.select { |pipeline| branch_name.casecmp(pipeline[:name]).zero? }
  if matched_pipelines.empty?
    raise Puppet::Error, "Could not find pipeline for #{pipeline_type} repository: #{repo_name} with branch name: #{branch_name}."
  end
  if matched_pipelines.length > 1
    raise Puppet::Error, "Found multiple pipelines for #{pipeline_type} repository: #{repo_name} with branch name: #{branch_name}."
  end
  matched_pipelines[0]
end

#get_pipelines_as_code_error(workspace, repo_type, repo_name) ⇒ Object



308
309
310
311
312
313
314
315
316
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 308

def get_pipelines_as_code_error(workspace, repo_type, repo_name)
  params = {
    op: 'GetPipelinesAsCodeError',
    get_repo_payload_key(repo_type) => repo_name,
  }
  api_uri = URI(get_ajax_endpoint(workspace))
  api_uri.query = URI.encode_www_form(params)
  make_request(:get, api_uri.to_s)
end

#list_agent_credentialsObject



589
590
591
592
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 589

def list_agent_credentials
  endpoint = "#{HW_CONFIG_ENDPOINT}?op=ListAgentCredentials"
  make_request(:get, endpoint)
end

#list_job_templates(workspace) ⇒ Object



286
287
288
289
290
291
292
293
294
295
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 286

def list_job_templates(workspace)
  # This does not use pagination because the frontend doesn't so this is consistent.
  # TODO: Use pagination here or add search functionality (CDPE-2280)
  params = {
    op: 'ListVmJobTemplates',
  }
  api_uri = URI(get_ajax_endpoint(workspace))
  api_uri.query = URI.encode_www_form(params)
  make_request(:get, api_uri.to_s)
end

#list_pipelines_by_name(workspace, repo_name, pipeline_type, branch_name) ⇒ Object



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 412

def list_pipelines_by_name(workspace, repo_name, pipeline_type, branch_name)
  params = {
    op: 'ListPipelinesByName',
    pipelineName: branch_name,
  }
  case pipeline_type
  when 'control'
    params[:controlRepoName] = repo_name
  when 'module'
    params[:moduleName] = repo_name
  else
    raise Puppet::Error "pipeline_type does not match one of: 'control', 'module'"
  end
  api_uri = URI(get_ajax_endpoint(workspace))
  api_uri.query = URI.encode_www_form(params)
  make_request(:get, api_uri.to_s)
end

#list_puppet_environments(workspace, creds_name) ⇒ Object



402
403
404
405
406
407
408
409
410
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 402

def list_puppet_environments(workspace, creds_name)
  params = {
    op: 'ListPuppetEnterpriseEnvironments',
    name: creds_name,
  }
  api_uri = URI(get_ajax_endpoint(workspace))
  api_uri.query = URI.encode_www_form(params)
  make_request(:get, api_uri.to_s)
end

#list_serversObject



594
595
596
597
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 594

def list_servers
  endpoint = "#{HW_CONFIG_ENDPOINT}?op=ListServers"
  make_request(:get, endpoint)
end

#list_source_orgs(workspace, provider) ⇒ Object



276
277
278
279
280
281
282
283
284
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 276

def list_source_orgs(workspace, provider)
  params = {
    op: 'ListSourceOrgs',
    provider: provider,
  }
  api_uri = URI(get_ajax_endpoint(workspace))
  api_uri.query = URI.encode_www_form(params)
  make_request(:get, api_uri.to_s)
end

#post_provider_webhook(workspace, repo) ⇒ Object



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 537

def post_provider_webhook(workspace, repo)
  repo_name = repo[:srcRepoName]
  # This is necessary due to business logic living in the UI code
  if repo[:srcRepoProvider].casecmp?('gitlab')
    repo_name = repo[:srcRepoId]
  end
  payload = {
    op: 'PostProviderWebhook',
    content: {
      srcRepoName: repo_name,
      srcRepoOwner: repo[:srcRepoOwner],
      srcRepoProvider: repo[:srcRepoProvider],
    },
  }
  make_request(:post, get_ajax_endpoint(workspace), payload.to_json)
end

#promote_pipeline_to_stage(workspace, repo_name, repo_type, branch_name, stage_name, commit_sha, commit_message) ⇒ Object



599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 599

def promote_pipeline_to_stage(workspace, repo_name, repo_type, branch_name, stage_name, commit_sha, commit_message)
  pipeline = get_pipeline_for_branch(workspace, repo_name, repo_type, branch_name)
  stage_index = CD4PEPipelineUtils.get_stage_index_by_name(pipeline[:stages], stage_name)
  unless pipeline[:buildStage] && pipeline[:buildStage][:imageEvent] 
    raise Puppet::Error "It looks like pipeline has not run before. Please run the pipeline and try promoting again."
  end
  
  payload = {
    op: 'PipelinePromote',
    content: {
      pipelineId: pipeline[:id],
      branch: branch_name,
      sha: commit_sha ? commit_sha : pipeline[:buildStage][:imageEvent][:commitId],
      stageNumber: stage_index + 1, # (stage_index starts with 0)
      commitMsg: commit_message ? commit_message : pipeline[:buildStage][:imageEvent][:commitMsg],
    }
  }
  if repo_type == 'control'
    payload[:content][:controlRepoName] = repo_name
  else
    payload[:content][:moduleName] = repo_name
  end

  make_request(:post, get_ajax_endpoint(workspace), payload.to_json)
end

#root_configObject



77
78
79
80
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 77

def root_config
  endpoint = "#{ROOT_AJAX_ENDPOINT}?op=GetRootConfiguration"
  make_request(:get, endpoint)
end

#save_endpoint_settings(webui, backend, agent) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 142

def save_endpoint_settings(webui, backend, agent)
  payload = {
    op: 'SaveEndpointSettings',
    content: {
      setting: {
        webUIEndpoint: webui,
        backendServiceEndpoint: backend,
        agentServiceEndpoint: agent,
      },
    },
  }
  make_request(:post, ROOT_ENDPOINT_SETTINGS, payload.to_json)
end

#save_license(license) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 95

def save_license(license)
  payload = {
    op: 'RootSavePfiLicense',
    content: license,
  }
  make_request(:post, ROOT_AJAX_ENDPOINT, payload.to_json)
end

#save_ssl_settings(ssl_authority_certificate, ssl_server_certificate, ssl_server_private_key, ssl_enabled) ⇒ Object



554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 554

def save_ssl_settings(ssl_authority_certificate, ssl_server_certificate, ssl_server_private_key, ssl_enabled)
  payload = {
    op: 'SaveSslSettings',
    content: {
      setting: {
        authorityCertificate: ssl_authority_certificate,
        serverCertificate: ssl_server_certificate,
        serverPrivateKey: ssl_server_private_key,
        sslEnabled: ssl_enabled,
      },
    },
  }
  make_request(:post, ROOT_AJAX_ENDPOINT, payload.to_json)
end

#save_storage_settings(provider, endpoint, bucket, prefix, access_key = nil, secret_key = '') ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 156

def save_storage_settings(provider, endpoint, bucket, prefix, access_key = nil, secret_key = '')
  # CDPE-1195 - CD4PE attempts to encrypt the secret even if using DISK storage. Send an
  # empty string for now if the caller passed in nil
  secret_key = '' if secret_key.nil?
  payload = {
    op: 'SaveStorageSettings',
    content: {
      setting: {
        osType: provider,
        osEndpoint: endpoint,
        osDiskRoot: '/disk',
        osBucket: bucket,
        osPrefix: prefix,
        osCredKey: access_key,
        osCredSecret: secret_key,
      },
    },
  }
  make_request(:post, ROOT_STORAGE_SETTINGS, payload.to_json)
end

#search_source_repos(workspace, source_control, repo_org, repo_name) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 262

def search_source_repos(workspace, source_control, repo_org, repo_name)
  params = {
    op: 'SearchSourceRepos',
    provider: source_control,
    search: repo_name,
  }
  unless repo_org[:personalOrg]
    params[:org] = repo_org[:organization]
  end
  api_uri = URI(get_ajax_endpoint(workspace))
  api_uri.query = URI.encode_www_form(params)
  make_request(:get, api_uri.to_s)
end


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
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 42

def set_cookie
  content = {
    op: 'PfiLogin',
    content: {
      email: @config[:email],
      passwd: @config[:password],
    },
  }

  response = make_request(:post, LOGIN_ENDPOINT, content.to_json)
  if response.code == '200'
    @cookie = response.response['set-cookie'].split(';')[0]
    JSON.parse(response.body, symbolize_names: true)
  elsif response.code == '401'
    begin
      resp = JSON.parse(response.body, symbolize_names: true)
      if resp[:error][:code] == 'LoginFailed'
        # Root account may not exist, try creating it
        response = 
        if response.code == '200'
          @cookie = response.response['set-cookie'].split(';')[0]
        else
          raise Puppet::Error, "Invalid login credentials to CD4PE host: #{@config[:server]}"
        end
      else
        raise Puppet::Error, "Invalid login credentials to CD4PE host: #{@config[:server]}"
      end
    rescue
      raise Puppet::Error, "Invalid login credentials to CD4PE host: #{@config[:server]}"
    end
  else
    raise Puppet::Error, "Invalid login credentials to CD4PE host: #{@config[:server]}"
  end
end

#set_is_build_pr_allowed(workspace, repo_name, repo_type, is_allowed) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 384

def set_is_build_pr_allowed(workspace, repo_name, repo_type, is_allowed)
  payload = {
    op: 'SetIsBuildPRAllowed',
    content: {
      isBuildPRAllowed: is_allowed,
    },
  }
  case repo_type
  when 'control'
    payload[:content][:controlRepoName] = repo_name
  when 'module'
    payload[:content][:moduleName] = repo_name
  else
    raise Puppet::Error "repo_type does not match one of: 'control', 'module'"
  end
  make_request(:post, get_ajax_endpoint(workspace), payload.to_json)
end

#set_pipeline_auto_build_triggers(workspace, repo_name, pipeline_type, pipeline_id, branch, triggers) ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 361

def set_pipeline_auto_build_triggers(workspace, repo_name, pipeline_type, pipeline_id, branch, triggers)
  payload = {
    op: 'SetPipelineAutoBuildTriggers',
    content: {
      pipelineId: pipeline_id,
      rule: {
        autoBuildTriggers: triggers,
        branch: branch,
      },
    },
  }

  case pipeline_type
  when 'control'
    payload[:content][:controlRepoName] = repo_name
  when 'module'
    payload[:content][:moduleName] = repo_name
  else
    raise Puppet::Error "pipeline_type does not match one of: 'control', 'module'"
  end
  make_request(:post, get_ajax_endpoint(workspace), payload.to_json)
end

#set_pipelines_as_code_branch(workspace, repo_type, repo_name, branch_name) ⇒ Object



297
298
299
300
301
302
303
304
305
306
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 297

def set_pipelines_as_code_branch(workspace, repo_type, repo_name, branch_name)
  payload = {
    op: 'SetPipelinesAsCodeBranch',
    content: {
      get_repo_payload_key(repo_type) => repo_name,
      branchName: branch_name,
    },
  }
  make_request(:post, get_ajax_endpoint(workspace), payload.to_json)
end

#upsert_pipeline_stages(workspace, repo_name, pipeline_type, pipeline_id, stages) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/puppet_x/puppetlabs/cd4pe_client.rb', line 344

def upsert_pipeline_stages(workspace, repo_name, pipeline_type, pipeline_id, stages)
  payload = {
    op: 'UpsertPipelineStages',
    content: {
      pipelineId: pipeline_id,
      stages: stages,
    },
  }

  if pipeline_type == 'control'
    payload[:content][:controlRepoName] = repo_name
  elsif pipeline_type == 'module'
    payload[:content][:moduleName] = repo_name
  end
  make_request(:post, get_ajax_endpoint(workspace), payload.to_json)
end