亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過應用程序腳本更新 GCP 元數據

通過應用程序腳本更新 GCP 元數據

GCT1015 2023-03-03 17:15:30
基本上我想在電子表格中創建一個按鈕,該按鈕將運行等效于以下命令gcloud compute project-info add-metadata --project ${project} --metadata "test_label=test_value"是否可以?我對谷歌 javascript 庫不是很熟悉。
查看完整描述

2 回答

?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

所以基本上感謝 Mateo 的指點,我能夠使用這個腳本更新項目元數據:


function alex_test_function() {

 // get existing oauth token

 var theAccessTkn = ScriptApp.getOAuthToken();

 // get existing project metadata

 var response = 

 UrlFetchApp.fetch('https://compute.googleapis.com/compute/v1/projects/myProject', {

   headers: {

     Authorization: 'Bearer ' + theAccessTkn

   }

 });

 var data = JSON.parse(response.getContentText());

 var metadata = data.commonInstanceMetadata


 fingerprint = metadata.fingerprint;

 new_metadata_items = metadata.items;


// update metadata

 var timestamp = new Date().getTime()

 setMetaKey(new_metadata_items, Session.getActiveUser().getEmail().split("@")[0], timestamp)


 var formData = {

  'fingerprint': fingerprint,

  'items': new_metadata_items

  };


 var postresponse = UrlFetchApp.fetch("https://compute.googleapis.com/compute/v1/projects/myProject/setCommonInstanceMetadata", {

  'method' : 'post',

  'contentType': 'application/json',

  'payload' : JSON.stringify(formData),

  'headers': {

    Authorization: 'Bearer ' + theAccessTkn

   }

  });

}


function setMetaKey(metadata, key, value){

  // Function to add metadata or update if exists

  for (var i = 0; i < metadata.length; i++) {

    if (metadata[i].key === key) {

      metadata[i].value = value;

      return;

    }

  }

  metadata.push({key:key, value:value});

}

一些陷阱,我們需要將 OAuth 范圍設置為 AppScript 清單


{

  "timeZone": "America/New_York",

  "dependencies": {

  },

  "exceptionLogging": "STACKDRIVER",

  "runtimeVersion": "V8",

  "oauthScopes": [

    "https://www.googleapis.com/auth/userinfo.email", 

    "https://www.googleapis.com/auth/compute", 

    "https://www.googleapis.com/auth/script.external_request"]

}

并且運行腳本的用戶需要具有編輯 GCP 項目中的項目元數據的權限。


我沒有對范圍進行很多實驗,可以用更窄的范圍而不是https://www.googleapis.com/auth/compute來執行腳本


查看完整回答
反對 回復 2023-03-03
?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

不幸的是,在回答這個問題時,Apps 腳本中沒有方法或類來更改 Google Cloud 項目的元數據。但是,您可以使用Properties Service類獲取和修改文檔、腳本或用戶元數據。

但是,如果您想以編程方式編輯 GCP 項目屬性,則需要使用Google Cloud API,因為它允許您從gcloud、其 API 或整個控制臺修改這些屬性。



查看完整回答
反對 回復 2023-03-03
  • 2 回答
  • 0 關注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號