क्लाउड फ़ाउंडेशन टूलकिट 101

1. सीएफ़टी 101 के बारे में जानकारी

b1d2ab0f35bb62a8.png

पिछली बार अपडेट किए जाने की तारीख: 2022-02-11

Cloud Foundation टूलकिट क्या है?

इसका मतलब है कि सीएफ़टी, Google Cloud Platform का इस्तेमाल तुरंत शुरू करने के लिए सबसे सही तरीके वाले टेंप्लेट उपलब्ध कराता है. इस ट्यूटोरियल में, आपको Cloud Foundation टूलकिट में योगदान देने का तरीका बताया गया है.

आपको इन चीज़ों की ज़रूरत होगी

  • एक GitHub खाता.
  • Docker अपनी मशीन पर इंस्टॉल करें या Cloud Shell का इस्तेमाल करें ( Mac इंस्टॉल, Windows इंस्टॉल)
  • कोड में बदलाव करने के लिए कोड एडिटर (उदाहरण: विज़ुअल स्टूडियो कोड)
  • Git और GitHub के बारे में बुनियादी जानकारी
  • कोड के तौर पर, टेरेस और इन्फ़्रास्ट्रक्चर का इस्तेमाल करने का कुछ अनुभव
  • सेवा खाते को प्रोजेक्ट क्रिएटर की भूमिका देने की अनुमति
  • Google Cloud संगठन, एक टेस्ट फ़ोल्डर, और एक बिलिंग खाता

आपको क्या बनाना होगा

इस कोडलैब की मदद से, आपको Cloud Foundation Toolkit (CFT) में योगदान देने का तरीका पता चलेगा.

ऐसा करने पर:

  • सीएफ़टी में योगदान देने के लिए, डेवलपर एनवायरमेंट को सेटअप करें
  • किसी सीएफ़टी मॉड्यूल में कोई सुविधा जोड़ें
  • जोड़ी गई सुविधा के लिए टेस्ट जोड़ें
  • सीएफ़टी में इंटिग्रेशन टेस्ट पूरा करें
  • लिंट टेस्ट एक्ज़ीक्यूट करें
  • GitHub पर कोड डालें और पुल का अनुरोध (पीआर) सबमिट करें

Google Cloud Storage सीएफ़टी मॉड्यूल में नई सुविधा जोड़कर, आपको ऊपर बताए गए सभी चरण पूरे करने होंगे. आपको "silly_label" नाम का एक लेबल जोड़ना होगा. यह लेबल, GCS (जीसीएस) सीएफ़टी मॉड्यूल के ज़रिए बनाए गए सभी बकेट में अपने-आप जुड़ जाएगा. आपको अपनी सुविधा की पुष्टि करने और इंटिग्रेशन को एंड-टू-एंड बनाने के लिए, लिखने के टेस्ट भी मिलेंगे.

2. डेवलपर एनवायरमेंट सेटअप करें

अगर आप चाहें, तो अपने डेवलपमेंट के मकसद से Cloud Shell का इस्तेमाल किया जा सकता है. अगर आपको सीएफ़टी में योगदान देने के लिए, Cloud Shell का इस्तेमाल नहीं करना है, तो अपनी मशीन पर डेवलपर एनवायरमेंट को सेट अप किया जा सकता है.

Git सेट अप करना

GitHub एक ओपन सोर्स वर्शन कंट्रोल सिस्टम (VCS) पर आधारित है, जिसे Git कहते हैं. GitHub से जुड़े हर उस काम के लिए Git ज़िम्मेदार है जो आपकी मशीन या आपके Cloud Shell पर स्थानीय तौर पर होता है.

  1. Cloud Shell का इस्तेमाल करने पर, आपको git इंस्टॉल करने की ज़रूरत नहीं है, क्योंकि यह पहले से इंस्टॉल होता है.
$ git --version
# This will display the git version on the Cloud Shell.

अगर आप अपनी मशीन पर अपना डेव एनवायरमेंट सेट अप कर रहे हैं, तो आपको Git इंस्टॉल करना होगा.

Git में अपना उपयोगकर्ता नाम और ईमेल सेट करना

Git किसी पहचान से कमिट को जोड़ने के लिए उपयोगकर्ता नाम का इस्तेमाल करता है. Git उपयोगकर्ता नाम आपके GitHub उपयोगकर्ता नाम से अलग है.

आप git कॉन्फ़िगरेशन कमांड का इस्तेमाल करके, उस नाम को बदल सकते हैं जो आपके Git से जुड़ा है. git config का इस्तेमाल करके, Git से किए जाने वाले लेन-देन का नाम बदलने पर, सिर्फ़ आने वाले समय में किए जाने वाले लेन-देन पर ही असर पड़ेगा. हालांकि, पहले किए गए कामों के लिए इस्तेमाल किए जाने वाले नाम में कोई बदलाव नहीं होगा.

आपने Git सफलतापूर्वक सेट कर लिया है और आपको ब्रांच बनाने, बनाने और क्लोन करने में सक्षम होना चाहिए. हम इस कोडलैब (कोड बनाना सीखना) में Git का भरपूर इस्तेमाल करेंगे.

3. फ़ोर्क CFT की GCS रिपॉज़िटरी

सीएफ़टी रिपॉज़िटरी को फ़ोर्क करना

आपने पहले चरण में अपनी लोकल मशीन या Cloud Shell पर Git सेट अप किया है. अब आपको योगदान देना शुरू करने के लिए, Google Cloud Storage सीएफ़टी रेपो को फ़ोर्क करना होगा.

फ़ोर्क, रिपॉज़िटरी की कॉपी होती है. रिपॉज़िटरी को फ़ोर्क करने से, ओरिजनल प्रोजेक्ट पर असर डाले बिना, बदलावों को आसानी से आज़माया जा सकता है.

अक्सर, फ़ोर्क का इस्तेमाल किसी और के प्रोजेक्ट में बदलाव का सुझाव देने के लिए किया जाता है या फिर किसी और के प्रोजेक्ट को अपने आइडिया के लिए शुरुआती पॉइंट के रूप में इस्तेमाल करने के लिए किया जाता है.

उदाहरण के लिए, किसी गड़बड़ी को ठीक करने से जुड़े बदलावों के सुझाव देने के लिए, फ़ोर्क का इस्तेमाल किया जा सकता है. किसी गड़बड़ी को ठीक करने के लिए:

  • रिपॉज़िटरी को फ़ोर्क करें.
  • समस्या ठीक करें.
  • प्रोजेक्ट के मालिक को पुल का अनुरोध सबमिट करें.

सीएफ़टी रेपो बनाने का तरीका:

  1. अपना वेब ब्राउज़र खोलें और terraform-google-modules/terraform-google-cloud-storage रिपॉज़िटरी पर जाएं. हम पूरे कोडलैब के लिए, इस रेपो का इस्तेमाल करेंगे.
  2. पेज के सबसे ऊपर दाएं कोने में, Fork पर क्लिक करें.

9dc18f15ca662b56.png

  1. आपको एक विकल्प दिखेगा जहां आपको फ़ोर्क रखना है. अपनी प्रोफ़ाइल चुनें और रेपो को फ़ॉर्क कर दिया जाएगा.

अपने फ़ोर्क को स्थानीय तौर पर क्लोन करें

आपका बनाया गया फ़ोर्क, GCS मॉड्यूल रिपॉज़िटरी की कॉपी है. अपनी नई सुविधा जोड़ने के लिए, अब आपको इस डेटा स्टोर करने की जगह को अपने लोकल एनवायरमेंट में क्लोन करना होगा.

अपने फ़ोर्क का क्लोन बनाने का तरीका:

  1. अपना वेब ब्राउज़र खोलें और terraform-google-modules/terraform-google-cloud-storage पर अपने फ़ोर्क पर नेविगेट करें.
  2. सबसे ऊपर दाएं कोने में आपको "कोड" दिखेगा बटन, उस पर क्लिक करें.

98f8be8df319dcd8.png

  1. "कोड" पर क्लिक करने के बाद बटन, "कॉपी करें" पर क्लिक करें फ़ोर्क का यूआरएल कॉपी करने के लिए आइकॉन का इस्तेमाल करें. इस यूआरएल का इस्तेमाल, अपने फ़ोर्क को अपने लोकल एनवायरमेंट में क्लोन करने के लिए किया जाएगा.

e61e1da6371f2a1d.png

  1. अपने VSCode या अपनी मशीन के किसी टर्मिनल पर जाएं और फ़ोर्क का क्लोन बनाएं.
$ git clone <url>
# This command will clone your fork locally.
# Paste the copied URL from the previous step.
  1. अब जबकि आपने अपने फ़ोर्क को स्थानीय तौर पर क्लोन बना लिया है, तो आपको अपने रेपो में जाना चाहिए, और फ़ोर्क से एक नई ब्रांच बनानी चाहिए और अस्थायी ब्रांच में कोड में बदलाव करना चाहिए.

नियम के मुताबिक, अपनी शाखा का नाम इस तरह रखा जा सकता है:

  • सुविधा के अनुरोधों के लिए: feature/feature-name
  • अंदरूनी अपडेट के लिए, internal/change-name
  • गड़बड़ी ठीक करने के लिए: bugfix/issue-name

नई सुविधा जोड़ी जा रही है. इसलिए, आपके पास अपने अस्थायी ब्रांच feature/silly_label को कॉल करने का विकल्प है

$ cd terraform-google-cloud-storage
# This command takes you into the cloned directory on your local machine.

$ git branch
# This command tells your current branch
# When you run this for the first time after you have cloned, your 
# output should say "master", that is your fork.

$ git checkout -b feature/silly_label
# This command creates a new branch on your fork and switches your 
# branch to the newly created branch.

$ git branch
# This command will confirm your current branch to be "feature/silly_label"

अब आप Cloud Foundation Toolkit पर काम शुरू करने के लिए तैयार हैं!

4. टेस्ट एनवायरमेंट बनाना

स्टैंडर्ड सीएफ़टी डेवलपमेंट की प्रोसेस, टेस्टिंग के लिए अलग-अलग टेस्ट प्रोजेक्ट का इस्तेमाल करने पर आधारित होती है. इस चरण में, सेवा खाते के ज़रिए टेस्ट प्रोजेक्ट (स्टैंडर्ड कॉन्फ़िगरेशन के आधार पर) बनाने के बारे में बताया जाएगा.

0. Docker Engine इंस्टॉल करें

अगर आपकी मशीन का इस्तेमाल डेवलपमेंट के लिए किया जा रहा है, तो आपको Docker Engine इंस्टॉल करना होगा.

1. Google Cloud SDK टूल इंस्टॉल करें

GCP Cloud Shell का इस्तेमाल करने पर, आपको Google Cloud SDK टूल इंस्टॉल करने की ज़रूरत नहीं होगी.

Google Cloud SDK पर जाएं और अपने प्लैटफ़ॉर्म के लिए इंटरैक्टिव इंस्टॉलर डाउनलोड करें.

2. कॉन्फ़िगरेशन सेट करें

टेस्ट एनवायरमेंट बनाने के लिए, आपके पास Google Cloud संगठन, एक टेस्ट फ़ोल्डर, और एक बिलिंग खाता होना चाहिए. इन वैल्यू को एनवायरमेंट वैरिएबल के ज़रिए सेट करना होगा:

export TF_VAR_org_id="your_org_id"
export TF_VAR_folder_id="your_folder_id"
export TF_VAR_billing_account="your_billing_account_id"

3. अपना सेवा खाता सेट अप करना

टेस्ट एनवायरमेंट बनाने से पहले, आपको टेस्ट एनवायरमेंट में सेवा खाते की कुंजी डाउनलोड करनी होगी. इस सेवा खाते के लिए, प्रोजेक्ट क्रिएटर, बिलिंग खाते के उपयोगकर्ता, और संगठन के दर्शक की भूमिकाएं होनी चाहिए. इन तरीकों से, नया सेवा खाता बनाने में मदद मिलती है. हालांकि, किसी मौजूदा खाते का फिर से इस्तेमाल भी किया जा सकता है.

3.1 सीड GCP प्रोजेक्ट बनाना या चुनना

सेवा खाता बनाने से पहले, उसे होस्ट करने के लिए कोई प्रोजेक्ट चुनना होगा. इसके अलावा, नया प्रोजेक्ट भी बनाया जा सकता है.

gcloud config set core/project YOUR_PROJECT_ID

3.2 Google Cloud API चालू करना

अपने सीड प्रोजेक्ट पर, इन Google Cloud API को चालू करें:

gcloud services enable cloudresourcemanager.googleapis.com
gcloud services enable iam.googleapis.com
gcloud services enable cloudbilling.googleapis.com

3.3 सेवा खाता बनाना

टेस्ट एनवायरमेंट को मैनेज करने के लिए, नया सेवा खाता बनाएं:

# Creating a service account for CFT.
gcloud iam service-accounts create cft-onboarding \
  --description="CFT Onboarding Terraform Service Account" \
  --display-name="CFT Onboarding"

# Assign SERVICE_ACCOUNT environment variable for later steps
export SERVICE_ACCOUNT=cft-onboarding@$(gcloud config get-value core/project).iam.gserviceaccount.com

पुष्टि करना कि आपका सेवा खाता बनाया गया है:

gcloud iam service-accounts list --filter="EMAIL=${SERVICE_ACCOUNT}"

3.4 सेवा खाते को प्रोजेक्ट क्रिएटर, बिलिंग खाते के उपयोगकर्ता, और संगठन दर्शक की भूमिकाएं देना:

gcloud resource-manager folders add-iam-policy-binding ${TF_VAR_folder_id} \
  --member="serviceAccount:${SERVICE_ACCOUNT}" \
  --role="roles/resourcemanager.projectCreator"
gcloud organizations add-iam-policy-binding ${TF_VAR_org_id} \
  --member="serviceAccount:${SERVICE_ACCOUNT}" \
  --role="roles/billing.user"
gcloud beta billing accounts add-iam-policy-binding ${TF_VAR_billing_account} \
  --member="serviceAccount:${SERVICE_ACCOUNT}" \
  --role="roles/billing.user"
gcloud organizations add-iam-policy-binding ${TF_VAR_org_id} \
  --member="serviceAccount:${SERVICE_ACCOUNT}" \
  --role="roles/resourcemanager.organizationViewer"

अब आपके पास ऐसा सेवा खाता है जिसका इस्तेमाल टेस्ट एनवायरमेंट को मैनेज करने के लिए किया जा सकता है.

4. टेराफ़ॉर्म क्रेडेंशियल तैयार करना

टेस्ट एनवायरमेंट बनाने के लिए, आपको अपने शेल में सेवा खाते की कुंजी डाउनलोड करनी होगी.

4.1 सेवा खाते की कुंजी

Teraform के लिए सेवा खाते की कुंजी बनाना और डाउनलोड करना

gcloud iam service-accounts keys create cft.json --iam-account=${SERVICE_ACCOUNT}

4.2 टेराफ़ॉर्म क्रेडेंशियल सेटअप करना

अपनी सेवा खाता कुंजी के कॉन्टेंट की वैल्यू सेट करते हुए, एनवायरमेंट वैरिएबल SERVICE_ACCOUNT_JSON का इस्तेमाल करके, Tenraform को कुंजी दें.

export SERVICE_ACCOUNT_JSON=$(< cft.json)

एनवायरमेंट वैरिएबल में क्रेडेंशियल की जानकारी सेव करने के बाद, कीफ़ाइल हटाएं. ज़रूरत पड़ने पर, ऊपर दिए गए इसी निर्देश का इस्तेमाल करके, कुंजी को फिर से बनाया जा सकता है.

rm -rf cft.json

5. टेराफ़ॉर्म के डिप्लॉयमेंट के लिए टेस्ट प्रोजेक्ट बनाना

अब सब कुछ तैयार है, इसलिए एक निर्देश से टेस्ट प्रोजेक्ट बनाया जा सकता है. terraform-google-cloud-storage डायरेक्ट्री रूट से यह कमांड चलाएं:

make docker_test_prepare

make docker_test_prepare चलाने पर आपको नीचे दिया गया आउटपुट दिखेगा. आखिर में , आपको जांच के लिए Project_id मिलेगा. इसे वहां बनाया गया है जहां Cloud Storage मॉड्यूल को नई सुविधा के साथ डिप्लॉय और टेस्ट किया जाएगा. अगर आपको बिलिंग खाता जोड़ने में समस्याएं आती हैं, तो समस्या हल करने का तरीका देखें.

macbookpro3:terraform-google-cloud-storage user$ make docker_test_prepare
docker run --rm -it \
                -e SERVICE_ACCOUNT_JSON \
                -e TF_VAR_org_id \
                -e TF_VAR_folder_id \
                -e TF_VAR_billing_account \
                -v /Users/cft/terraform-google-cloud-storage:/workspace \
                gcr.io/cloud-foundation-cicd/cft/developer-tools:0.8.0 \
                /usr/local/bin/execute_with_credentials.sh prepare_environment
Activated service account credentials for: [cft-onboarding@<project_id>.iam.gserviceaccount.com]
Activated service account credentials for: [cft-onboarding@<project_id>.iam.gserviceaccount.com]
Initializing modules...

Initializing the backend...

Initializing provider plugins...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.google-beta: version = "~> 3.9"
* provider.null: version = "~> 2.1"
* provider.random: version = "~> 2.2"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
module.project.module.project-factory.null_resource.preconditions: Refreshing state... [id=8723188031607443970]
module.project.module.project-factory.null_resource.shared_vpc_subnet_invalid_name[0]: Refreshing state... [id=5109975723938185892]
module.project.module.gsuite_group.data.google_organization.org[0]: Refreshing state...
module.project.module.project-factory.random_id.random_project_id_suffix: Refreshing state... [id=rnk]
module.project.module.project-factory.google_project.main: Refreshing state... [id=<project-id>]
module.project.module.project-factory.google_project_service.project_services[0]: Refreshing state... [id=<project-id>/storage-api.googleapis.com]
module.project.module.project-factory.google_project_service.project_services[1]: Refreshing state... [id=<project-id>/cloudresourcemanager.googleapis.com]
module.project.module.project-factory.google_project_service.project_services[2]: Refreshing state... [id=<project-id>/compute.googleapis.com]
module.project.module.project-factory.data.null_data_source.default_service_account: Refreshing state...
module.project.module.project-factory.google_service_account.default_service_account: Refreshing state... [id=projects/ci-cloud-storage-ae79/serviceAccounts/project-service-account@<project-id>.iam.gserv
iceaccount.com]
module.project.module.project-factory.google_project_service.project_services[3]: Refreshing state... [id=<project-id>/serviceusage.googleapis.com]
module.project.module.project-factory.null_resource.delete_default_compute_service_account[0]: Refreshing state... [id=3576396874950891283]
google_service_account.int_test: Refreshing state... [id=projects/<project-id>/serviceAccounts/cft-onboarding@<project-id>.iam.gserviceaccount.com]
google_service_account_key.int_test: Refreshing state... [id=projects/<project-id>/serviceAccounts/cft-onboarding@<project-id>.iam.gserviceaccount.com/keys/351009a1e011e88049ab2097994d1c627a61
6961]
google_project_iam_member.int_test[1]: Refreshing state... [id=<project-id>/roles/iam.serviceAccountUser/serviceaccount:cft-onboarding@<project-id>.iam.gserviceaccount.com]
google_project_iam_member.int_test[0]: Refreshing state... [id=<project-id>/roles/storage.admin/serviceaccount:cft-onboarding@<project-id>.iam.gserviceaccount.com]

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

project_id = <test-project-id>
sa_key = <sensitive>
Found test/setup/make_source.sh. Using it for additional explicit environment configuration.

अब आपने एक टेस्ट प्रोजेक्ट बना लिया है, जिसका रेफ़रंस campaign_id में दिया गया है, जैसा कि कंसोल आउटपुट में देखा जा सकता है. आपका डेवलपमेंट और टेस्ट एनवायरमेंट सेट अप हो गया है.

5. सीएफ़टी मॉड्यूल में नई सुविधा जोड़ें

अब आपका डेवलपमेंट और टेस्ट एनवायरमेंट सेटअप हो गया है, अब अपना "silly_label" जोड़ें सुविधा को google-cloud-storage CFT मॉड्यूल में जोड़ा.

पक्का करें कि आप terraform-google-cloud-storage में हैं और मुख्य.tf फ़ाइल खोलें, जैसा कि नीचे फ़ोल्डर संरचना में दिखाया गया है.

ac1dba25408abd09.png

"silly_label" की वजह से एक लेबल है, तो आपको "लेबल" वैरिएबल में लाइन 27 में सुविधा जोड़नी होगी मुख्य.tf में, जैसा कि आपको नीचे दिखाया गया है:

terraform-google-cloud-storage/main.tf

resource "google_storage_bucket" "buckets" {
 <...>
 storage_class = var.storage_class
 // CODELAB:Add silly label in labels variable
 labels        = merge(var.labels, { name = replace("${local.prefix}${lower(each.value)}", ".", "-") }, { "silly" = var.silly_label })
 force_destroy = lookup(
 <...>
}

अब, ऊपर दिए गए फ़ोल्डर स्ट्रक्चर में दिखने वालेवैरिएबल.tf में silly_label वैरिएबल जोड़ें.

नीचे दिए गए कोड को कॉपी करके, वैरिएबल.tf में लाइन 31 में जोड़ें. साथ ही, पक्का करें कि जोड़े गए वैरिएबल ब्लॉक के ऊपर और नीचे एक नया लाइन वर्ण मौजूद हो.

terraform-google-cloud-storage/variables.tf

variable "names" {
 description = "Bucket name suffixes."
 type        = list(string)
}

// CODELAB: Add "silly_label" variable to variables.tf between "names" and "location"
variable "silly_label" {
 description = "Sample label for bucket."
 type        = string
}

variable "location" {
 description = "Bucket location."
 default     = "EU"
}

6. स्टोरेज बकेट के उदाहरण के लिए, एक नई सुविधा जोड़ें

आपने अपनी सुविधा को मॉड्यूल के key.tf में जोड़ दिया है और अब आप एक उदाहरण के ज़रिए, जोड़ी गई सुविधा की जांच करेंगे.

"silly_label" को example/multi-buckets/main.tf में जोड़ना होगा

इस उदाहरण का इस्तेमाल, अगले चरण में इंटिग्रेशन की जांच करने के लिए किया जाएगा.

नीचे दी गई वैरिएबल silly_label लाइन को कॉपी करके, item.tf में लाइन 27 पर चिपकाएं. यहां आपको terraform-google-cloud-storage/examples/multiple-buckets/ जैसा कुछ फ़ोल्डर में बताया गया है:

5224fefbbcc61d89.png

terraform-google-cloud-storage/examples/multiple-buckets/main.tf

module "cloud_storage" {
 <...>
 // CODELAB: Add "silly_label" as an example to main.tf.
 silly_label        = "awesome"

 <..>
}

7. सुविधा देखने के लिए, ब्लूप्रिंट टेस्ट अपडेट करें

आपने अपनी सुविधा को मॉड्यूल के custom.tf में जोड़ा है और फिर सुविधा को Multi_buckets उदाहरण में जोड़ा है. अब आपको अपनी सुविधा को Golang में लिखे गए ब्लूप्रिंट इंटिग्रेशन टेस्ट के ज़रिए टेस्ट करना होगा.

आप अपनी नई जांच को यहां दिए गए फ़ोल्डर स्ट्रक्चर में मौजूद Multi_buckets_test.go फ़ाइल में जोड़ेंगे:

72ea272d4792405.png

आपने "silly_label" जोड़ा हो मल्टीपल_buckets मॉड्यूल से बनाए जा रहे सभी बकेट पर लागू होते हैं और अब आपको नई सुविधा को टेस्ट करने के लिए टेस्ट लिखना होगा.

नीचे दिए गए कोड में, आपको gcloud अल्फ़ा स्टोरेज कमांड से हर बकेट का लेबल मिल रहा है. इसके बाद, आपको कमांड से मिलने वाले आउटपुट की जांच करनी होगी.

test/integration/multiple_buckets/multiple_buckets_test.go

func TestMultipleBuckets(t *testing.T) {
 <..>
op := gcloud.Run(t, fmt.Sprintf("alpha storage ls --buckets gs://%s", bucketName), gcloudArgs).Array()[0]

// verify silly label on each bucket
assert.Equal("awesome", op.Get("metadata.labels.silly").String(), "should have silly label set to awesome")

// verify lifecycle rules
...
}

8. सीएफ़टी में इंटिग्रेशन टेस्ट करें

इंटिग्रेशन की जांच

इंटिग्रेशन टेस्ट का इस्तेमाल, रूट मॉड्यूल, सबमॉड्यूल, और उदाहरणों के काम करने के तरीके की पुष्टि करने के लिए किया जाता है. परीक्षणों के साथ संयोजन, परिवर्तन और सुधार भी शामिल होने चाहिए.

इंटिग्रेशन टेस्ट, ब्लूप्रिंट टेस्ट फ़्रेमवर्क का इस्तेमाल करके लिखे जाते हैं और सीएफ़टी सीएलआई का इस्तेमाल करके चलाए जाते हैं. इन टूल को सुविधा के लिए Docker इमेज में ही पैकेज किया जाता है.

इन टेस्ट की सामान्य रणनीति, उदाहरण मॉड्यूल के काम करने के तरीके की पुष्टि करना है. इससे यह पक्का करने में मदद मिलती है कि रूट मॉड्यूल, सबमॉड्यूल, और उदाहरण मॉड्यूल सही तरीके से काम कर रहे हैं.

इंटरैक्टिव एक्ज़ीक्यूशन में, हर चरण को एक से ज़्यादा कमांड से लागू किया जाता है.

  1. इंटरैक्टिव मोड में Docker कंटेनर की जांच शुरू करने के लिए, make docker_run चलाएं.

Make एक बिल्ड ऑटोमेशन टूल है, जो Makefiles नाम की फ़ाइलों को पढ़कर सोर्स कोड से एक्ज़ीक्यूटेबल प्रोग्राम और लाइब्रेरी तैयार करता है. इन प्रोग्राम में, टारगेट प्रोग्राम को बनाने का तरीका बताया जाता है. फ़ाइल में बदलाव करने पर, डॉकर कंटेनर अपने-आप अपडेट हो जाना चाहिए.

make docker_run चलाने पर, डॉक कंटेनर में एक फ़ाइल फ़ोल्डर बनाया जाता है और अपने सेवा खाते के लिए क्रेडेंशियल को चालू किया जाता है. अगले चरण में, टेस्ट करने के लिए फ़ाइल फ़ोल्डर का इस्तेमाल किया जाएगा.

आपको अपने टर्मिनल में यह आउटपुट दिखेगा:

Activated service account credentials for: [cft@<PROJECT_ID>.iam.gserviceaccount.com]
  1. अपने फ़ाइल फ़ोल्डर में सभी ब्लूप्रिंट टेस्ट की सूची बनाने के लिए cft test list चलाएं.

आपको अपने टर्मिनल में यह आउटपुट दिखेगा:

[root@CONTAINER_ID workspace]# cft test list
 NAME                           | CONFIG                    | LOCATION                                                   
--------------------------------+---------------------------+------------------------------------------------------------
 TestAll/examples/simple_bucket | examples/simple_bucket    | test/integration/discover_test.go                          
 TestMultipleBuckets            | examples/multiple_buckets | test/integration/multiple_buckets/multiple_buckets_test.go 

  1. उदाहरण शुरू करने के लिए, cft test run <EXAMPLE_NAME> --stage init चलाएं. इस मामले में, TestMultipleBuckets टेस्ट रन शुरू करने के लिए, cft test run TestMultipleBuckets --stage init. साथ ही, टेस्ट चलाते समय ज़्यादा जानकारी पाने के लिए, --verbose फ़्लैग का इस्तेमाल भी किया जा सकता है.

यह इनिट स्टेज, टेराफ़ॉर्म के उदाहरण को शुरू करता है.

आपको अपने टर्मिनल में नीचे दिया गया आउटपुट दिखेगा.

[root@<CONTAINER_ID> workspace]# cft test run TestMultipleBuckets --stage init --verbose
INFO[02-09|08:24:31] using test-dir: test/integration 
...
TestMultipleBuckets 2022-02-09T08:24:35Z command.go:179: Terraform has been successfully initialized!
...
TestMultipleBuckets 2022-02-09T08:24:35Z command.go:100: Running command terraform with args [validate]
TestMultipleBuckets 2022-02-09T08:24:36Z command.go:179: Success! The configuration is valid.
...
--- PASS: TestMultipleBuckets (4.05s)
  1. उदाहरण के तौर पर दिया गया मॉड्यूल लागू करने के लिए, cft test run <EXAMPLE_NAME> --stage apply चलाएं.

यह चरण, पिछले चरण में शुरू किए गए उदाहरण को कोडलैब में पहले बनाए गए GCP प्रोजेक्ट पर लागू करता है.

आपको अपने टर्मिनल में नीचे दिया गया आउटपुट दिखेगा.

[root@<CONTAINER_ID> workspace]# cft test run TestMultipleBuckets --stage apply --verbose
INFO[02-09|08:28:11] using test-dir: test/integration
...
TestMultipleBuckets 2022-02-09T08:28:19Z command.go:179: Apply complete! Resources: 6 added, 0 changed, 0 destroyed.
TestMultipleBuckets 2022-02-09T08:28:19Z command.go:179: 
TestMultipleBuckets 2022-02-09T08:28:19Z command.go:179: Outputs:
TestMultipleBuckets 2022-02-09T08:28:19Z command.go:179: 
TestMultipleBuckets 2022-02-09T08:28:19Z command.go:179: names = {
TestMultipleBuckets 2022-02-09T08:28:19Z command.go:179:   "one" = "multiple-buckets-erp1-eu-one"
...
--- PASS: TestMultipleBuckets (6.51s)
PASS
ok      github.com/terraform-google-modules/terraform-google-cloud-storage/test/integration/multiple_buckets    6.548s
  1. cft test run <EXAMPLE_NAME> --stage verify चलाएं, ताकि इस बात की पुष्टि की जा सके कि उदाहरण में उम्मीद के मुताबिक इन्फ़्रास्ट्रक्चर बनाया गया है.

यह चरण, पुष्टि करने की सुविधा को TestMultipleBuckets में चलाएगा. आम तौर पर, gcloud कमांड की मदद से पुष्टि की जाती है. इससे, किसी रिसॉर्स की मौजूदा स्थिति के लिए JSON आउटपुट मिलता है. साथ ही, यह भी पुष्टि की जाती है कि मौजूदा स्थिति वही है जो दिए गए उदाहरण में दी गई है.

अगर आपको कोई गड़बड़ी मिलती है, तो आप यह देख पाएंगे कि टेस्ट के लिए क्या उम्मीद की गई थी और कमांड से क्या मिला.

आपको अपने टर्मिनल में नीचे दिया गया आउटपुट दिखेगा.

cft test run TestMultipleBuckets --stage verify --verbose
INFO[02-09|08:30:19] using test-dir: test/integration
...
TestMultipleBuckets 2022-02-09T08:30:27Z command.go:100: Running command terraform with args [output -no-color -json names_list]
TestMultipleBuckets 2022-02-09T08:30:27Z command.go:179: ["multiple-buckets-erp1-eu-one","multiple-buckets-erp1-eu-two"]
TestMultipleBuckets 2022-02-09T08:30:27Z command.go:100: Running command gcloud with args [alpha storage ls --buckets gs://multiple-buckets-erp1-eu-one --project ci-cloud-storage-8ce9 --json]
TestMultipleBuckets 2022-02-09T08:30:28Z command.go:179: [
TestMultipleBuckets 2022-02-09T08:30:28Z command.go:179: {
TestMultipleBuckets 2022-02-09T08:30:28Z command.go:179:   "url": "gs://multiple-buckets-erp1-eu-one/",
...
TestMultipleBuckets 2022-02-09T08:30:33Z command.go:179: ]
2022/02/09 08:30:33 RUN_STAGE env var set to verify
2022/02/09 08:30:33 Skipping stage teardown
--- PASS: TestMultipleBuckets (12.32s)
PASS
ok      github.com/terraform-google-modules/terraform-google-cloud-storage/test/integration/multiple_buckets    12.359s
  1. उदाहरण को टियर करने के लिए cft test run <EXAMPLE_NAME> --stage teardown चलाएं.

यह चरण उस इंफ़्रास्ट्रक्चर को नष्ट कर देता है जो आपने ऊपर के चरणों में बनाया था. यह चरण प्रोजेक्ट में बनाए गए GCS बकेट को भी खत्म कर देगा. इसके अलावा, यह उस लेबल को भी मिटा देगा जिसे आपने GCS मॉड्यूल में जोड़ा है.

अपने टर्मिनल में, नीचे दिया गया आउटपुट देखा जा सकता है.

[root@<CONTAINER_ID> workspace]# cft test run TestMultipleBuckets --stage teardown --verbose
INFO[02-09|08:36:02] using test-dir: test/integration
...
TestMultipleBuckets 2022-02-09T08:36:06Z command.go:100: Running command terraform with args [destroy -auto-approve -input=false -lock=false]
TestMultipleBuckets 2022-02-09T08:36:07Z command.go:179: module.cloud_storage.random_id.bucket_suffix: Refreshing state... [id=mNA]
TestMultipleBuckets 2022-02-09T08:36:07Z command.go:179: random_string.prefix: Refreshing state... [id=erp1]
TestMultipleBuckets 2022-02-09T08:36:08Z command.go:179: module.cloud_storage.google_storage_bucket.buckets["two"]: Refreshing state... [id=multiple-buckets-erp1-eu-two]
...
TestMultipleBuckets 2022-02-09T08:36:10Z command.go:179: Destroy complete! Resources: 6 destroyed.
TestMultipleBuckets 2022-02-09T08:36:10Z command.go:179: 
--- PASS: TestMultipleBuckets (6.62s)
PASS
ok      github.com/terraform-google-modules/terraform-google-cloud-storage/test/integration/multiple_buckets    6.654s
  1. टेस्ट कंटेनर से बाहर निकलने के लिए, exit चलाएं.

9. इनपुट और आउटपुट के लिए दस्तावेज़ जनरेट करना

रूट मॉड्यूल, सबमॉड्यूल, और उदाहरण मॉड्यूल के README में इनपुट और आउटपुट टेबल, उनसे जुड़े मॉड्यूल के variables और outputs के आधार पर अपने-आप जनरेट होते हैं. मॉड्यूल के इंटरफ़ेस में बदलाव होने पर, इन टेबल को रीफ़्रेश करना ज़रूरी है.

चलाएं:

make generate_docs
# This will generate new Inputs and Outputs tables

10. सीएफ़टी में लिंट टेस्ट करें

लिंटर एक ऐसा टूल है जो प्रोग्रामिंग गड़बड़ियों, बग, स्टाइल से जुड़ी गड़बड़ियों, और संदिग्ध कंस्ट्रक्शन को फ़्लैग करने के लिए सोर्स कोड का विश्लेषण करता है.

क्वालिटी के मानक को बनाए रखने के लिए, रिपॉज़िटरी में मौजूद कई फ़ाइलों को लिंट किया जा सकता है या फ़ॉर्मैट किया जा सकता है. सीएफ़टी की क्वालिटी पक्का करने के लिए, लिंट टेस्ट का इस्तेमाल करें.

चलाएं:

make docker_test_lint
# This will run all lint tests on your repo

11. GitHub पर पीआर सबमिट करना

आपने कोड को स्थानीय तौर पर बदल दिया है और इंटिग्रेशन टेस्ट में उसकी जांच कर ली है. अब आपको यह कोड मास्टर रेपो में पब्लिश करना है.

मास्टर रेपो पर अपना कोड उपलब्ध कराने के लिए, आपको अपनी ब्रांच में कोड बदलाव करने होंगे और उसे मास्टर रिपॉज़िटरी में भेजना होगा. कोडलैब की शुरुआत में फ़ोर्क किए गए मुख्य रिपो में आपके कोड को जोड़ने के लिए, आपको मास्टर रेपो पर पुल रिक्वेस्ट (पीआर) देना होगा. इसके लिए, आपको कोड बनाना होगा.

पीआर बढ़ाने पर, रेपो एडमिन को प्रस्तावित कोड में बदलावों की समीक्षा करने के लिए सूचित किया जाएगा. इसके अलावा, अपने कोड में हुए बदलावों के बारे में सुझाव पाने के लिए, दूसरे उपयोगकर्ताओं को भी समीक्षकों के तौर पर जोड़ा जा सकता है. पीआर से एक Cloud Build ट्रिगर होगा, जो रेपो पर जांच करेगा.

आपके कोड में होने वाले बदलावों के आधार पर, कोड समीक्षक आपके कोड पर टिप्पणियां देंगे. साथ ही, अगर सबसे सही तरीकों और दस्तावेज़ के आधार पर किसी बदलाव की ज़रूरत है, तो कोड में बदलाव करने के लिए कहेंगे. एडमिन आपके कोड में किए गए बदलावों की समीक्षा करेगा और यह पक्का करेगा कि आपका कोड, रेपो के नियमों का पालन करता हो. साथ ही, आपके कोड को मास्टर रेपो में मर्ज करने से पहले, एडमिन आपसे कुछ बदलाव करने का अनुरोध कर सकता है.

अपनी फ़ोर्क की गई ब्रांच में कोड डालने और बाकी ब्रांच में पुश कोड डालने के लिए, यह तरीका अपनाएं:

  1. सबसे पहले, बदली गई फ़ाइलें लोकल रेपो में जोड़ें.
$ git add main.tf
$ git add README.md
$ git add variables.tf
$ git add examples/multiple-buckets/main.tf
$ git add test/integration/multiple_buckets/multiple_buckets_test.go
# The ‘git add' command adds the file in the local repository and 
# stages the file for commit. To unstage a file, use git reset HEAD YOUR-FILE
  1. अब आपकी फ़ाइलों की जांच की जा चुकी है. इसके बाद, आपको बदलाव करने होंगे.
$ git commit -m "First CFT commit"
# This will commit the staged changes and prepares them to be pushed 
# to a remote repository. To remove this commit and modify the file, 
# use 'git reset --soft HEAD~1' and commit and add the file again.
  1. पुल का अनुरोध (पीआर) बनाने के लिए, अपने लोकल डेटा स्टोर करने की जगह में किए गए बदलावों को GitHub में भेजें.
$ git push -u origin feature/silly_label
# Pushes the changes in your local repository up to the remote
# repository you specified as the origin

आपके कोड में किए गए बदलाव, अब पुल के अनुरोध के लिए तैयार हैं!

terraform-google-modules/terraform-google-cloud-storage रेपो में पीआर बनाने के लिए यह तरीका अपनाएं:

  1. अपने वेब ब्राउज़र में, रेपो के मुख्य पेज पर जाएं.
  2. आपको अपने फ़ोर्क से पीआर खोलने के लिए बैनर में एक सुझाव दिखेगा. "अनुरोध करें और पुल करें" पर क्लिक करें.

60e7ae0cbc11588e.png

  1. कोड में हुए बदलावों के बारे में जानकारी देने के लिए, पुल के अनुरोध का टाइटल और ब्यौरा डालें. कम से कम शब्दों में सटीक जानकारी दें.

329342f7e9d64410.png

  1. समीक्षा के लिए तैयार पुल का अनुरोध करने के लिए, "पुल का अनुरोध बनाएं" पर क्लिक करें.
  2. आपको ऐसे क्लाउड बिल्ड ट्रिगर दिखेंगे जो पीआर की वजह से ट्रिगर हुए हैं.
  3. अगर आपको कोई समस्या आती है, तो फॉर्क से पुल अनुरोध खोलने के बारे में जानने के लिए GitHub के आधिकारिक दस्तावेज़ देखें.

आपने कोड में किए गए पहले बदलाव को मुख्य ब्रांच में बदल दिया और अपने पहले सीएफ़टी पीआर को मास्टर ब्रांच पर बढ़ा दिया!

12. बधाई हो

बधाई हो, आपने सीएफ़टी मॉड्यूल में कोई सुविधा जोड़ दी है और पीआर को समीक्षा के लिए सबमिट कर दिया है!

आपने सीएफ़टी मॉड्यूल में किसी सुविधा को जोड़ा, एक उदाहरण के ज़रिए उसे स्थानीय तौर पर टेस्ट किया, और GitHub पर अपना कोड डालने से पहले जांच की. आखिर में, आपने समीक्षा के लिए पीआर इकट्ठा किया और उसे सीएफ़टी में शामिल किया.

अब आपको Cloud Foundation Toolkit का इस्तेमाल शुरू करने के ज़रूरी चरणों के बारे में पता है.