


package mainimport ("context""encoding/json""fmt""github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common""github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors""github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"config "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/config/v20220802""github.com/tencentyun/scf-go-lib/cloudfunction")// maingofunc main() {cloudfunction.Start(ReceiveMessage)}type Tag struct {TagKey stringTagValue string}type ClientContext struct {InvokingEvent *InvokingEventRuleParameters map[string]string // Rule parametersResultToken string // Encryption tokenOrderingTimestamp int64 // Evaluation timestamp}type InvokingEvent struct {TriggerType stringConfigurationItem *ConfigurationItem}type ConfigurationItem struct {AccountId int64 // ID of the user to whom the resource belongsRegion string // Resource regionZone string // Resource AZConfiguration string // Detailed resource configurationResourceCreationTime int64 // Resource creation timestampResourceType string // Resource typeResourceId string // Resource IDResourceStatus string // Resource IDResourceName string // Resource nameTags []Tag // Resource tagsCaptureTime int64 // Resource snapshot timestamp}// ReceiveMessage receives consumption messages {// "InvokingEvent": {// "TriggerType": "MANUAL",// "ConfigurationItem": {// "AccountId": 100004293724,// "Region": "ap-guangzhou",// "Zone": "",// "Configuration": { // The fields within the Configuration object vary by resource type// "CidrBlock": "172.16.0.0/16",// "IsDefault": true,// "Ipv6CidrBlock": "",// "DnsServerSet": null,// "DomainName": "",// "DhcpOptionsId": ""// },// "ResourceCreationTime": 1520431078,// "ResourceType": "QCS::VPC::Vpc",// "ResourceId": "vpc-q252nx9j",// "ResourceStatus": "",// "ResourceName": "Default-VPC",// "Tags": null,// "CaptureTime": 1686500243// }// },// "RuleParameters": {// "234": "324"// },// "ResultToken": "Wm9yZlY3WmlKa3cxaW1oQpgtklO2shRhG1gtxnC4qyszJtkSz5ZpZDshF6YyaaIAagGsEAcidC-VFNJHtRfXfam_FHMR_RhhelEAT4ApsKFQIBEZNaWN284dZS02f7uRT6w_zwHz5E1dXmNYvancDRgiCQHip_uUrif0Toypdbh1tuLODHgVN1csbaPKu3hb2-O-PBh824HACVUkDXJAp2KMQnqhNagmlUULjY-GMyM=",// "OrderingTimestamp": 1686537830//}func ReceiveMessage(ctx context.Context, event ClientContext) error {eventStr, _ := json.Marshal(event)fmt.Println("SCF:ReceiveMessage:Event:", string(eventStr))putRuleResule(event)return nil}// Custom compliance check for resources --- To evaluate resources, you must implement the evaluation logic based on your specific business needs, returning COMPLIANT or NON_COMPLIANT. The following code is for reference only.func getComplianceType(configurationStr string) string {return "COMPLIANT"}func putRuleResule(event ClientContext) {evaluations := make([]*config.Evaluation, 0)//1: Set the evaluation result. The format must comply with the following example requirements.complianceType := getComplianceType(event.InvokingEvent.ConfigurationItem.Configuration)configuration := "xxxx"desiredValue := "xxxxx"evaluation := &config.Evaluation{ComplianceResourceId: &event.InvokingEvent.ConfigurationItem.ResourceId,ComplianceResourceType: &event.InvokingEvent.ConfigurationItem.ResourceType,ComplianceRegion: &event.InvokingEvent.ConfigurationItem.Region,ComplianceType: &complianceType,Annotation: &config.Annotation{Configuration: &configuration,DesiredValue: &desiredValue,},}evaluations = append(evaluations, evaluation)//2: Report the evaluation result// Requires the Config_QCSLinkedRoleInConfigRecorder service role permissioncredential := common.NewCredential("xxxx","xxxxx",)cpf := profile.NewClientProfile()cpf.HttpProfile.Endpoint = "config.internal.tencentcloudapi.com"client, _ := config.NewClient(credential, "ap-guangzhou", cpf)request := config.NewPutEvaluationsRequest()request.ResultToken = &event.ResultTokenrequest.Evaluations = evaluationsresponse, err := client.PutEvaluations(request)if _, ok := err.(*errors.TencentCloudSDKError); ok {fmt.Printf("An API error has returned: %s", err)return}if err != nil {panic(err)}// Output a string packet in JSON format.fmt.Printf("%s", response.ToJsonString())}





Apakah halaman ini membantu?
Anda juga dapat Menghubungi Penjualan atau Mengirimkan Tiket untuk meminta bantuan.
masukan